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;
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.
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: