此C ++代码旨在将文件中的数据从67〜69移动到70〜72:
#include <fstream>
#include <iostream>
int main() {
std::fstream file("test", std::ios::out);
file.close();
file.open("test", std::ios::binary | std::ios::in | std::ios::out);
for (char i = 0; i < 127; ++i)
file.write(&i, 1);
file.seekp(70);
file.seekp(-3, std::ios::cur);
char s[100];
for (int i = 0; i < 100; ++i)
s[i] = '\0';
file.read(s, 3);
for (int i = 0; i < 3; ++i)
std::cout << (int)s[i] << " ";
std::cout << std::endl;
file.write(s, 3);
file.seekp(-3, std::ios::cur);
file.read(s, 3);
for (int i = 0; i < 3; ++i)
std::cout << (int)s[i] << " ";
std::cout << std::endl;
}
我两次阅读s
的内容应该相同,就像我们在g ++中编译代码时的输出一样:
67 68 69
67 68 69
但是Visual Studio中的结果不同:
67 68 69
70 71 72
是否存在导致错误的不确定行为?我该如何解决?