This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: tellg modifying the state of an input file stream
Paolo Carlini wrote:
... and that's why: there is no mention in the C++ Standard of \r\n line
endings and such. Is something is going wrong has to do with the way
MingW deals with those sequences, at the C library level too. Honestly,
however, I think something is badly broken there, not a simple,
localized, bug, because nobody could use C++ on MingW, otherwise.
Because of that, I would also not exclude something broken on your
system. Again, please do your check and get in touch with the MingW
maintainers, first.
Indeed, it seems the problem is more related to the C standard library (which as I understand is implemented by MinGW?), not the C++ library. Another reason I thought it was the C++ library was the fact that the same example with fopen/ftell works fine. I decided to test that after seeing in libstdc++ code that it uses FILE* I/O. Here's the program text again:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream ofs( "streamtest.txt" );
ofs << "Test" << endl << "Test" << endl;
ofs.close();
#if 01 // streams
ifstream ifs( "streamtest.txt" );
char c;
ifs.get(c);
while ( ! ifs.fail() ) {
unsigned p = ifs.tellg();
cout << "c = " << c << ", pos: " << p << endl;
ifs.get(c);
}
ifs.close();
#else // FILE*
FILE* fs = fopen( "streamtest.txt", "r" );
char c;
do {
c = fgetc( fs );
cout << "c = " << c << ", pos: " << ftell( fs ) << endl;
} while ( ! feof( fs ) );
fclose(fs);
#endif
return 0;
}
I searched the MinGW list archives, comp.lang.c++.moderated and GCC's bug tracker. The problem has been reported before (e.g. here http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13333 , here http://sourceforge.net/mailarchive/message.php?msg_id=598854 and here https://sourceforge.net/tracker/index.php?func=detail&aid=765526&group_id=2435&atid=102435 ), but I didn't quite understand the explanation... it was blamed on MS for some reason. Something like their fgetc or other part of the C library implementation doing something wrong with line endings. However, as I tested above, it seems fgetc/ftell is working fine in MinGW.
I'll post this on MinGW's list again to ask for an explanation. Because Earnie Boyd's statement (in the second link) that "You can't seek and tell on a text mode file, it's not a bug, it's a defined feature" doesn't seem right to me. And there are other C and C++ library implementations which don't have this problem on Windows. Even though the C++ standard doesn't talk about line endings, I had the impression that the streams library should work correctly on files with *consistent* line endings, no matter if they are \n or \r\n.
Thanks,
Ivan