简体   繁体   English

在C ++中将输出写入文件的问题

[英]Issues with writing output to file in C++

I am writing to a file in C++ in append mode, the program which I am using for the same is: 我在附加模式下用C ++写一个文件,我使用的程序是:

#include <fstream>
void main()
{
 ofstream f;
 f.open("f.txt", ios::app);
 f<<"\n Hello";
 f.close();
}

Now the output which is getting printed in the output file is some thing junk...which I can't comprehend: 现在输出文件中打印的输出是垃圾......我无法理解:

  OUTPUT:
  牐湩㩴

Please help me as to where am i going wrong??? 请帮我,我哪里出错了??? I am working on linux. 我正在研究linux。

The file which you are appending to has a BOM marker indicating it is UTF-16 encoded. 您要附加的文件具有BOM标记,表明它是UTF-16编码的。 Recreate the file using an editor which will not encode the file, or use a program to write it from scratch. 使用不编码文件的编辑器重新创建文件,或使用程序从头开始编写文件。

This is because you didn't specify a text encoding, and in the absence of explicit encoding markings, Windows just guesses. 这是因为您没有指定文本编码,并且在没有显式编码标记的情况下,Windows只是猜测。 The most famous instance of this is Bush Hid The Facts 最着名的例子是Bush Hid The Facts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM