This code:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
int main()
{
std::remove("test.txt");
std::fstream f("test.txt",std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc);
std::cout << f.good() << std::endl;
f<<"test"<< std::flush;
std::cout << f.tellg() << " " << f.tellp() << std::endl;
f.seekg(0);
std::string s;
f>>s;
std::cout << f.tellg() << " " << f.tellp() << std::endl;
}
Gives following output in gcc-4.4.5
1
4 4
4 4
i.e. both tellg and tellp returned expected stream position 4.
While gcc-4.6.0
Gives:
1
4 4
-1 4
Where can I find a reference to tell:
- 1st case is correct (bug in gcc-4.6)
- 2nd case is correct (bug in gcc < gcc-4.6)
- Both case are correct the behavior is undefined
Ok, it is not a bug, even it seems that it is required behavior:
According to C++ 2003 standard:
tellg(): (27.6.1.3)
sentry (27.6.1.1.2):
So basically
So gcc-4.6 seems to behave correctly…