Microsoft KB Archive/307384

= How to read a text file by using System.IO and Visual C++ =

Article ID: 307384

Article Last Modified on 8/28/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-



This article was previously published under Q307384



This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.IO
 * System.Collections

IN THIS TASK
SUMMARY
 * Requirements
 * Reading Text Files in Visual C++
 * Complete Code Listing
 * Troubleshooting

REFERENCES



SUMMARY
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.

back to the top

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

back to the top

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 &quot;Hello World&quot; 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(&quot;c:\\test.txt&quot;);   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 = &quot;&quot;; 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; } objReader->Close;   Finally, output the contents of your newly filled ArrayList to the console by using a &quot;For Each&quot; loop. for(int i = 0; i<arrText->Count; i++) Console::WriteLine(arrText->Item[i]->ToString); Console::ReadLine; </li> Save and run your code, which will produce a listing of your file out to the console.</li></ol>

back to the top

Complete Code Listing
using namespace System; using namespace System::IO; using namespace System::Collections;
 * 1) using <mscorlib.dll>

int wmain(void) int main(void) {   StreamReader* objReader = new StreamReader(&quot;c:\\test.txt&quot;); String *sLine = &quot;&quot;; ArrayList *arrText = new ArrayList; sLine = objReader->ReadLine; while (sLine != 0) {       arrText->Add(sLine); sLine = objReader->ReadLine; }   objReader->Close;
 * 1) ifdef _UNICODE
 * 1) else
 * 1) endif

//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; } back to the top

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 &quot;Try...Catch&quot; block to handle these common problems.

back to the top

<div class="references_section">