I am creating a program (In C++) that takes an ASCII file and reads a few values from each line until it reaches the end of the file. I am using ifstream
to read the file, and I have never had problems with it stopping when I use the ifstream.eof()
method. This time, however, even though it found the eof character in my test case, when I analyzed my other files, it is infinite looping because it never finds the eof character. Is this a coding issue, or an issue with my files?
string line = "";
unsigned long pos = 0;
ifstream curfile(input.c_str());
getline(curfile, line);
int linenumber = 0;
cout<<"About to try to read the file"<<endl;
if (!curfile.good())
cout<<"Bad file read"<<endl;
while (!curfile.eof())
{
cout<<"Getting line "<<linenumber<<endl;
linenumber++;
pos = line.find_first_of(' ');
line = line.substr(pos+1, line.size()-1);
pos = line.find_first_of(' ');
current.push_back(atof(line.substr(0, pos).c_str()));
for (int i = 0; i<4; i++)
{
pos = line.find_first_of(' ');
line = line.substr(pos+1, line.size()-1);
}
pos = line.find_first_of(' ');
dx.push_back(atof(line.substr(0, pos).c_str()));
pos = line.find_first_of(' ');
line = line.substr(pos+1, line.size()-1);
pos = line.find_first_of(' ');
dy.push_back(atof(line.substr(0, pos).c_str()));
getline(curfile, line);
}
EDIT: When I first run the loop, currentfile.good() returns false...what am I doing that causes it to return that?
Do not do it like that.
EOF
is not the only thing you'll encounter while reading. There's a bunch of errors you might get, and so the best is to simply test the stream itself:If you're reading lines, then, the simplest way is:
First thing is first, you shouldn't check like that.
eof()
doesn't returntrue
until after a failed read. But you can do better (and easier)!check the stream state with the implicit conversion to
void*
which can be used in abool
context. Since most of the read operations on streams return a reference to the stream, you can write some very consice code like this:Basically what it is doing is saying "while I could successfully extract a line from
currentfile
, do the following", which is what you really meant to say anyway ;-);Like I said, this applies to most stream operations, so you can do things like this:
EDIT: The way I would rewrite your code is like this:
Your first call to
getline
is triggering one of the fail-bits on theifstream
object. That is why if you do a check for a fail-bit usingios::good()
, you never enter your read loop. I would check to see what the value ofline
is ... it's probably empty, meaning you're having another issue reading your file, like maybe permissions problems, etc.The problem is here:
Try this: