eofbit lost?

hi all!

i want to have a look at several sequences of information, (highly probable) stored in c-strings in lines of a recuring scheme in a *.ytc. therefore i put some relevant lines in a *.txt in order to seek my favourites via c-string features. the problem is descibed below the (reduced) code:

#include <fstream>
#include <iostream>
using namespace std;

int main(){
system( "grep XB /../land.ytc > /../land.txt");
ifstream docFile( "./land.txt");

if ( docFile.is_open())
{
while ( !docFile.eof())
{
cout << " anything";
}
}
docFile.close();
return 0;
}

executing this will produce an endless loop because the eofbit is not set in my land.txt, i guess.
or the other way round: when i have a look at land.txt with less, everything is fine, ie. it appears a resonable number of lines. but executing my above code with some additions to read the lines via ifstream::getline() and so on will produce the original file for a number of resonable lines, but after 'passing the end' of land.txt it rolls on and on.

has anybody an idea what's going on and how to solve that?
cheers
Marcel
as wriiten above - It will be an endless loop because you are not reading anything from the file, so the file read pointer does not advance through the file - so eof will always fail.
Last edited on
I don't know exactly what is going on, but you shouldn't loop on eof() because the stream might go into a fail() or bad() state and you won't know about it. You can just use the stream itself as the condition, like this:

1
2
3
4
while(docFile)
{
    // code
}
hi all,

looping just using the stream or alternatively the good() member function is working out.

thanks a lot!!
@filipe: what would be you proposal to figure out what's going on? do i need to know more about grep?
@zelli

You don't read anything from the file. So you will never reach the end of the file. That's what is going wrong as guestgulkan has already pointed out.
Topic archived. No new replies allowed.