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 :)

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.