Hello. I'm writing program wich reads from file line by line and then rewrites everything to other file. This is simplifyied part of code:

for (int i=0; i<=5;) {
	getline (file1, str1); /* file1 is file from wich i read and str1 is string*/
	file << str1;
}

No i'm not copying file :) Between reading and writing i'm working with lines and rewriting to other file reformed text. But the problem is that i don't know how many lines are in file. So i need to catch file end to break loop. Please help me to catch file end. Thank you :)

Dani AI

Generated

Short summary and practical tips

The reliable way to stop at end-of-file is to attempt a read and test its result, not to test eof() before reading. As pointed out the read operation itself indicates success/failure, and is correct that looping on eof() can lead to processing the last line twice. Put the read in the loop condition (so the loop exits when the read fails) and avoid for (!file.eof()) style loops.

Checklist and common pitfalls

  • Always confirm the input file opened successfully before reading.
  • std::getline discards the newline character; write a newline back if the output must preserve line breaks.
  • A final line that lacks a terminating newline is still returned by a successful read — the read/test-in-loop approach handles that correctly.
  • If the goal is a plain copy without per-line processing, copying the buffer (out << in.rdbuf()) is simpler and faster.
  • I/O streams do not throw on EOF by default; enabling exceptions is possible but EOF is normally treated as an expected condition, not an exception.

MFC notes and cross-platform issues

  • Standard C++ streams (ifstream/ofstream) work inside MFC apps. In Unicode builds prefer the wide-character variants or convert between std::string/std::wstring and CString as needed.
  • If files are opened in binary mode or come from mixed platforms, trim a stray CR left from CRLF lines; for example:
    if (!line.empty() && line.back() == '\r') line.pop_back();

    This prevents a trailing '\r' from corrupting comparisons or output.

Credit
This complements the thread: correctly noted testing the read result, and correctly warned about eof() misuse and showed placing the read in the loop condition.

Recommended Answers

All 6 Replies

I'm not sure, too lazy to check it, but I believe getline function returns false when it fails, in your case file ends. Of course catching an EOF exception is better...

I tried to check return value but i couldn't find right variable type... I tried intiger, string.... always error... maybe you know wich type i need?

false is a boolean type, so you can just check it with

if( !getline(file1, str1) )
{ 
 //EOF
}

Of course catching an EOF exception is better...

I have never tried that before but I checked forum and i think i should use it like this:

for (!file1.eof()) {
	getline (file1, str1); /* file1 is file from wich i read and str1 is string*/
	file << str1;
}

Am i doing it right? And will this work on MFC project?
P.S. I'm not too lazy to check it but i want to be sure that my program will work fine... ;)

false is a boolean type, so you can just check it with

if( !getline(file1, str1) )
{ 
 //EOF
}

Thank you it's working perfect .

>>Am i doing it right?

No. The code you posted will try to read the last line of the file twice.

ifstream in("inpufile.txt");
ofstream out("newfile.txt");
while( getline (in, str1) )
{
    out << str1 << '\n';
}

>> And will this work on MFC project?
Yes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.