C++ FileIO奇怪的行为

Ron*_*Ron 2 c++ file-io ofstream windows-8.1

在编写程序时,我遇到了一个奇怪的行为std::ofstream.请参阅下面的代码

ofstream dire;
dire.open("dir.txt", std::ios::out);

// some other code

for(int i=0;i<dir.size();i++)
{
    dire << dir[i] << "\t"; // dir is integer vector containing values between 0-9
}
Run Code Online (Sandbox Code Playgroud)

现在,当我打开dir.txt内容时:

????????????????????????????????????????????????????????????.. and so on
Run Code Online (Sandbox Code Playgroud)

如果我给一个空格,然后选项卡(\ t)然后它正常工作或为此\n也正常工作. dire << dir[i] << " \t";

现在的输出是:

4   4   4   4   5   5   5   5   5   5.. and so on
Run Code Online (Sandbox Code Playgroud)

我也尝试dire.flush()将输出缓冲区刷新到文件,但结果仍然相同.

我绝对可以通过使用逃脱,\t但我想知道为什么会这样.

Luc*_*ian 6

如果你使用记事本来查看文件,那么布什隐藏事实的错误就是问题所在.

当字符串传递给Win32字符集检测函数IsTextUnicode而没有其他字符时,会发生错误.IsTextUnicode看到它认为有效的UTF-16LE中文并返回true,然后应用程序错误地将文本解释为UTF-16LE.