-
Parsing/EOF() problem
Hi.
The problem i have is with a program that is using while(!eof()) to read a file, and do actions based on the first character of a line.
Here is a very simple version of my code:
Code:
while(!DFile.eof())
{
DFile.getline(templine,1);
cout<<templine;
}
system("PAUSE");
the problem is, the program runs up to this point, then when it gets to the while loop the cursor just sits there blinking, and the program does not continue no matter how long i wait or what buttons i push. all i can do is shut down the program. Any help as to why this is happening?
-
woops, didn't log in correctly :rolleyes:
-
Try putting a cout<<endl; between the end of the loop and the pause statement.
-
Doesn't work.
here's what i did:
Code:
while(!DFile.eof())
{
DFile.getline(templine,1);
cout<<templine;
cout<<endl;
}
system("PAUSE");
the program now prints blank lines forever, never reaching the pause statement as if it's not reaching the end of the file. The file isn't large at all, so i don't know why it isnt getting to the end of it.
I also tried putting the cout<<endl outside of the bracket right before the pause statement, but that didn't do anything at all
-
What is templine declared as? And what compiler/OS are you using?
-
using win98, and dev-c++ v4.9.7.5 compiling with mingw32
-
Use this:
Code:
while( !DFile.eof( ) ) {
DFile.getline( templine, 72 ); // Changed 1 to 72
cout<<templine<<endl;
}
The second parameter for getline is the size of the buffer, so you should set it to 72. If the line is longer than buffersize - 1 characters, then it seems to crash.
-
>>while( !DFile.eof( ) ) {
This is not the way to control the loop. Use the return code from the read function to determine when you've hit EOF.
>>DFile.getline(templine,1);
Read the manual about the getline() function, as XSquared said, using 1 as the second parameter is incorrect.
http://www.cppreference.com/cppio_details.html#getline