While writing code to update the position in the binary, I noticed something strange. Consider this code example:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
char tmp;
string s;
fstream fs;
fs.open("test.txt", fstream::out);
fs << "blub" << endl;
fs.close();
fs.open("test.txt", fstream::in);
fs >> s;
cout << s << endl;
fs.close();
fs.open("test.txt", ios::in|ios::out|ios::binary);
if (!fs.is_open() || !fs.good())
cerr << "could not open!" << endl;
fs.read(&tmp, 1);
fs.read(&tmp, 1);
const char *c = "ah";
fs.write(&c[0], 1);
fs.write(&c[1], 1);
fs.close();
fs.open("test.txt", fstream::in);
fs >> s;
cout << s << endl;
}
In recent g ++ versions (at least from 6.2.1) I can just read and then write a few bytes without any problems. In the example, you will get the correct output:
blub
blah
Then I compiled the code with g ++ 4.7.2 and suddenly the update has no effect, i.e. the second output is still "blub", unless I add fs.tellg () or fs.tellp (). I found this question, but as I understand it, this is a limitation under Windows, but I work under Linux.
, g++, , g++, ? : ?
!