tellg modifying the state of an input file stream

Ivan Kolev ikolev@abac.com
Sat May 20 14:34:00 GMT 2006


Hi,

I have an example which looks like a bug. I use gcc 3.4.5 for MinGW on Windows. Here is the code:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	ofstream ofs( "streamtest.txt" );
	ofs << "Test" << endl; // <1>
	ofs.close();

	ifstream ifs( "streamtest.txt" );
	char c;
	ifs.get(c);
	while ( ! ifs.fail() ) {
		unsigned p = ifs.tellg();  // <2>
		cout << "c = " << c << ", pos: " << p << endl;
		ifs.get(c);
	}
	ifs.close();
}


This program prints
c = T, pos: 2
c = s, pos: 4
c =
, pos: 6

If either endl is not written to the file at line <1> or tellg() is not called at line <2> the output is as expected, i.e.:
c = T
c = e
c = s
c = t

Regards,
Ivan




More information about the Libstdc++ mailing list