Click to See Complete Forum and Search --> : how to close the program which read a file?
xmasry
April 28th, 2005, 02:55 AM
The following is my program. I don't know how to end the do... while loop.
By the way, Could you tell me how to read the file line by line? And, every line is read as CString? I just use fopen() and fgets(),so I read the line as char[]. I feel there are many problem when deal with char[]. I use VC++.NET.
Thanks!
g_gili
April 28th, 2005, 01:39 PM
You can try this
CStringFile sfText;
CString szLine;
// When the given file can be opened
if(sfText.Open(szFile))
{
// Read all the lines (one by one)
while(sfText.GetNextLine(szLine)!=0)
{
printf("%s\r\n",szLine); //And print them
}
sfText.Close(); // Close the opened file
cilu
April 29th, 2005, 11:51 AM
Or, for managed C++ you can use StreamReader:
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
void main()
{
try
{
StreamReader* sr = new StreamReader(S"TestFile.txt");
try
{
String* line;
while (line = sr->ReadLine())
Console::WriteLine(line);
}
__finally
{
if (sr) __try_cast<IDisposable*>(sr)->Dispose();
}
}
catch (Exception* e)
{
Console::WriteLine(S"The file could not be read:");
Console::WriteLine(e->Message);
}
}
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.