У меня есть простая программа:
#include <cstring>
#include <fstream>
using namespace std;
int main(int argc, char **argv)
{
cout << "Creating test.txt file..." << endl;
// Writing to file
fstream fWrite;
fWrite.open("./_test_data/test.txt", fstream::out | fstream::trunc);
if (fWrite.fail()) {
cout << "Creating test.txt file failed!" << endl;
fWrite.close();
} else {
fWrite << (char) 0x09 << (char) 0x0A << (char) 0x0B;
fWrite << (char) 0x0C << (char) 0x0D << (char) 0x0E;
fWrite << flush;
fWrite.close();
cout << "test.txt file successfully created." << endl;
}
// Reading created file
cout << "Reading test.txt file..." << endl;
fstream fRead;
fRead.open("./_test_data/test.txt", fstream::in);
if (fRead.fail()) {
fRead.close();
} else {
char character;
while (true) {
fRead >> character;
if (fRead.eof()) {
cout << (int)character << endl;
cout << "EOF detected!" << endl;
break;
}
cout << (int)character << endl;
}
fRead.close();
}
return 0;
}
Он должен просто записывать байты в порядке 09 0A 0B 0C 0D 0E
, что сделано нормально (проверено шестнадцатеричным дампом), но при чтении того же файла он читает первый байт как 0E
(= 14 в десятичной системе), а затем приходит EOF...
Creating test.txt file...
test.txt file successfully created.
Reading test.txt file...
14
14
EOF detected!
Почему?