C++ FileIO 奇怪的行为

标签 c++ file-io windows-8.1 ofstream

在编写程序时,我遇到了一个奇怪的行为 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/

相关文章:

c++ - "iterator cannot be defined in the current scope"错误

c++ - 通过 RS232 串行端口从条码扫描器读取异步数据

Java创建新对象的性能

C#/WinRT : How to save image to file

Windows 8.1 x64 上的 Delphi 6 Update 2 安装解决方法?

xaml - 如何设置 HubSection 标题样式

C++ XLST 转换无法使用 MSXML 3.0

c++ - 常量在这里真的合适吗,还是有另一种方法? - C++

c++ - 使用 ostream_iterator 将映射复制到文件中

c++ - 使用 getline 从文件读取输入