How to set file encoding format to UTF8 in C++

后端 未结 4 923
慢半拍i
慢半拍i 2021-01-13 03:59

A requirement for my software is that the encoding of a file which contains exported data shall be UTF8. But when I write the data to the file the encoding is always ANSI. (

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-13 04:20

    The type char has no clue of any encoding, all it can do is store 8 bits. Therefore any text file is just a sequence of bytes and the user must guess the underlying encoding. A file starting with a BOM indicates UTF 8, but using a BOM is not recommended any more. The type wchar_t in contrast is in Windows always interpreted as UTF 16.

    So let's say you have a file encoded in UTF 8 with just one line: "Confucius says: Smile. 孔子说:微笑!

提交回复
热议问题