问题
I have UTF-8 text file , that I'm reading using simple :
ifstream in("test.txt");
Now I'd like to create a new file that will be UTF-8 encoding or Unicode.
How can I do this with ofstream or other?
This creates ansi Encoding.
ofstream out(fileName.c_str(), ios::out | ios::app | ios::binary);
回答1:
Ok, about the portable variant. It is easy, if you use the C++11 standard (because there are a lot of additional includes like "utf8", which solves this problem forever).
But if you want to use multi-platform code with older standards, you can use this method to write with streams:
- Read the article about UTF converter for streams
- Add
stxutif.hto your project from sources above Open the file in ANSI mode and add the BOM to the start of a file, like this:
std::ofstream fs; fs.open(filepath, std::ios::out|std::ios::binary); unsigned char smarker[3]; smarker[0] = 0xEF; smarker[1] = 0xBB; smarker[2] = 0xBF; fs << smarker; fs.close();Then open the file as
UTFand write your content there:std::wofstream fs; fs.open(filepath, std::ios::out|std::ios::app); std::locale utf8_locale(std::locale(), new utf8cvt<false>); fs.imbue(utf8_locale); fs << .. // Write anything you want...
来源:https://stackoverflow.com/questions/5026555/c-how-to-write-read-ofstream-in-unicode-utf8