This step-by-step article shows how to retrieve information from a text file and use an
ArrayList to display that information to the user by using managed extensions for Visual C++ .NET or Visual C++ 2005.
Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
- Microsoft Visual C++ .NET or Microsoft Visual C++ 2005
Reading Text Files in Visual C++
Opening and reading files for read access is an important part of IO functionality, even if you do not need to write to the file in question. This example opens a file for reading. This is useful for reading text files but does not work for binary files. This example uses one of the many methods that are available for opening the file. Although many data structures are available for storing information retrieved from the file, an ArrayList is the easiest to use. To open and read from the file, you use objects from the
System.IO namespace, specifically the
System.IO.StreamReader class.
NOTE: For this example, you will need a text (.txt) file to read from.
To try loading and reading through a text file from Visual C++, follow these steps:
- Start Visual Studio .NET or Visual Studio 2005. Create a new Managed C++ application. Visual Studio will create a simple "Hello World" application.
- Make sure at least the System namespace is referenced by the project. Use the using namespace statement on the System, System.IO, and System.Collections namespaces so you will not be required to qualify declarations from these namespaces later in your code. These statements must be used prior to any other declarations.
using namespace System;
using namespace System::IO;
using namespace System::Collections;
- To open a file for reading, you can simply create a new instance of a StreamReader object and pass the file's path into the constructor.
StreamReader* objReader = new StreamReader("c:\\test.txt");
- You will need a string variable to store each line of the file as you process it, and because you will be adding these lines to an ArrayList you should declare and create an object of that type as well.
String *sLine = "";
ArrayList *arrText = new ArrayList();
- To read the file in, you have several choices, including a method named ReadToEnd which would read the entire file in at once, but for this example you can use the ReadLine method to bring the file in one line at a time. When the end of the file is reached, this method will return a null, which provides a way to end your loop. As you read each line from the file, you can insert them into your ArrayList by using the ArrayList's Add method.
sLine = objReader->ReadLine();
while (sLine != 0)
{
arrText->Add(sLine);
sLine = objReader->ReadLine();<BR/>
}
objReader->Close();
- Finally, output the contents of your newly filled ArrayList to the console by using a "For Each" loop.
for(int i = 0; i<arrText->Count; i++)
Console::WriteLine(arrText->Item[i]->ToString());
Console::ReadLine();
- Save and run your code, which will produce a listing of your file out to the console.
Complete Code Listing
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
using namespace System::Collections;
#ifdef _UNICODE
int wmain(void)
#else
int main(void)
#endif
{
StreamReader* objReader = new StreamReader("c:\\test.txt");
String *sLine = "";
ArrayList *arrText = new ArrayList();
sLine = objReader->ReadLine();
while (sLine != 0)
{
arrText->Add(sLine);
sLine = objReader->ReadLine();
}
objReader->Close();
//Write out the ArrayList to the console.
for(int i = 0; i<arrText->Count; i++)
Console::WriteLine(arrText->Item[i]->ToString());
Console::ReadLine();
return 0;
}
Troubleshooting
There are several things to be aware of when working with file I/O. Any time you are doing file access, there is the possibility that the file you are attempting to read or write may not be on the system or may be in use. You are also reading the entire file into memory before processing it, and you might run into a situation where the file is too large to be held in memory. Or, you might not have permission to access the file. Any of these situations will cause an exception to be raised. It is good practice to always provide a "Try...Catch" block to handle these common problems.