在编写程序时,我遇到了一个奇怪的行为 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
}
现在当我打开 dir.txt
内容是:
ऴऴऴऴवववववववववशशशशशशशशशशषषषषषषषषरररररररऱऱऱऱऱऱऱऱऱललललललललललललळ.. and so on
如果我先给一个空格然后用 tab(\t) 那么它就可以正常工作,或者就此而言\n 也可以正常工作。 dire << dir[i] << " \t";
现在输出是:
4 4 4 4 5 5 5 5 5 5.. and so on
我也试过dire.flush()
将输出缓冲区刷新到文件,但结果仍然相同。
我绝对可以使用 \t
逃脱但我想了解为什么会这样。
最佳答案
如果您使用记事本查看文件,则错误 Bush hid the facts可能是问题所在。
The bug occurs when the string is passed to the Win32 charset detection function IsTextUnicode with no other characters. IsTextUnicode sees what it thinks is valid UTF-16LE Chinese and returns true, and the application then incorrectly interprets the text as UTF-16LE.
关于C++ FileIO 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29094006/