This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
fstream.ignore() bug?
- To: gcc at gcc dot gnu dot org
- Subject: fstream.ignore() bug?
- From: Robert Schweikert <rjschwei at mindspring dot com>
- Date: Tue, 09 Jan 2001 20:15:01 -0500
There appears to be a problem with the ignore method for an fstream when
the end of file is reached, the attached example shows the problem.
-> g++ -DBUG -o bugTes streamTest.C
Now running bugTes leads to an endless loop (stopped artificially by a
counter) not defining BUG during compile shows the work around.
What I expect to happen is that the ignore method ignores extracts the
'\0' (last line in file), discards it and moves the file pointer to the
EOF, then the next read from the file should fail and the fail bit
should be set, thus my while loop should end. For some reason when using
the ignore method EOF is never reached, this seems incorrect.
Comments, questions help is appreciated.
Thanks,
Robert
--
Robert Schweikert MAY THE SOURCE BE WITH YOU
rjschwei@mindspring.com LINUX
This_is_line_1 should_this_be_ignored
This_is_line_2_only_end_of_line_should_be_ignored
This is line 3
#include <iostream.h>
#include <strstream.h>
#include <fstream.h>
int main()
{
fstream istream("readMe.txt", (ios::in | ios::nocreate));
int counter(0);
while (!istream.fail()) {
strstreambuf os;
istream.get(os);
#if defined(BUG)
istream.ignore();
#else
char fakBuf;
istream.get(fakBuf);
#endif
char* buf = os.str();
cerr << "read this: " << os.str() << endl;
delete [] buf;
if (istream.fail()){
cerr << "istream failed" << endl;
return 0;
}
else if (istream.eof()){
cerr << "found eof" << endl;
return 0;
}
else {
counter++;
cerr << "istream did not fail" << endl;
}
if (counter == 10) return 1;
}
}