0

I am writing to a file in C++ in append mode, the program which I am using for the same is:

#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.

4
  • 1
    It's because you need to convert the text you are outputting to UTF-8 first. Commented Jul 6, 2012 at 3:31
  • www2.research.att.com/~bs/bs_faq2.html#void-main Commented Jul 6, 2012 at 3:34
  • Interestingly enough, if you translate that from Chinese to English in Google Translate it says something about breast-feeding. Commented Jul 6, 2012 at 3:38
  • void main() is not C++. Please make it int main() Commented Jul 6, 2012 at 3:45

2 Answers 2

1

The file which you are appending to has a BOM marker indicating it is UTF-16 encoded. Recreate the file using an editor which will not encode the file, or use a program to write it from scratch.

Sign up to request clarification or add additional context in comments.

Comments

1

This is because you didn't specify a text encoding, and in the absence of explicit encoding markings, Windows just guesses. The most famous instance of this is Bush Hid The Facts

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.