简体   繁体   English

如何在Windows中用Windows编写UTF-8编码的字符串到文件中

[英]How do I write a UTF-8 encoded string to a file in windows, in C++

I have a string that may or may not have unicode characters in it, I am trying to write that to a file on windows. 我有一个字符串,可能有也可能没有unicode字符,我试图将其写入Windows上的文件。 Below I have posted a sample bit of code, my problem is that when I fopen and read the values back out windows, they are all being interpreted as UTF-16 characters. 下面我发布了一些示例代码,我的问题是当我fopen并将值读回窗口时,它们都被解释为UTF-16字符。

char* x = "Fool";
FILE* outFile = fopen( "Serialize.pef", "w+,ccs=UTF-8");
fwrite(x,strlen(x),1,outFile);
fclose(outFile);

char buffer[12];
buffer[11]=NULL;
outFile = fopen( "Serialize.pef", "r,ccs=UTF-8");
fread(buffer,1,12,outFile);
fclose(outFile);

The characters are also interpreted as UTF-16 if I open the file in wordpad etc. What am I doing wrong? 如果我在wordpad等中打开文件,字符也会被解释为UTF-16。我做错了什么?

Yes, when you specify that the text file should be encoded in UTF-8, the CRT implicitly assumes that you'll be writing Unicode text to the file. 是的,当您指定文本文件应以UTF-8编码时,CRT隐式假定您将Unicode文本写入文件。 Not doing so doesn't make sense, you wouldn't need UTF-8. 不这样做没有意义,你不需要UTF-8。 This will work proper: 这将正常工作:

wchar_t* x = L"Fool";
FILE* outFile = fopen( "Serialize.txt", "w+,ccs=UTF-8");
fwrite(x, wcslen(x) * sizeof(wchar_t), 1, outFile);
fclose(outFile);

Or: 要么:

char* x = "Fool";
FILE* outFile = fopen( "Serialize.txt", "w+,ccs=UTF-8");
fwprintf(outFile, L"%hs", x);
fclose(outFile);

It is easy if you use the C++11 standard (because there are a lot of additional includes like "utf8" which solves this problems forever). 如果你使用C++11标准很容易(因为有很多额外的包括像"utf8" ,它永远解决了这个问题)。

But if you want to use multi-platform code with older standards, you can use this method to write with streams: 但是,如果要使用旧标准的多平台代码,可以使用此方法使用流写入:

  1. Read the article about UTF converter for streams 阅读有关流的UTF转换器的文章
  2. Add stxutif.h to your project from sources above 从上面的源添加stxutif.h到您的项目
  3. Open the file in ANSI mode and add the BOM to the start of a file, like this: 以ANSI模式打开文件并将BOM添加到文件的开头,如下所示:

     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(); 
  4. Then open the file as UTF and write your content there: 然后将文件作为UTF打开并在那里写下您的内容:

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

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

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