简体   繁体   English

使用C ++用字节顺序标记(BOM)编写csv文件吗?

[英]Write csv files with Byte Order Mark (BOM) using c++?

I am trying to create a csv file with UTF8 with BOM using c++. 我正在尝试使用c ++使用带有BOM的UTF8创建一个csv文件。 Can anybody help me, how to do this? 谁能帮我,怎么做?

Simply put the BOM into a string and write that to your file (using whatever method you're using, eg streams or classic cstdio): 只需将BOM表放入字符串中并将其写入文件(使用您使用的任何方法,例如流或经典cstdio):

const char *bom = "\xef\xbb\xbf"; // UTF-8
const char *bom_alt = {0xef, 0xbb, 0xbf, 0}; // the null termination is optional, depending on how you write it later on

Then you'll just have to ensure you're actually writing proper UTF-8 and no ANSI or wide char strings. 然后,只需确保您实际上在编写正确的UTF-8,并且没有ANSI或宽字符字符串即可。 I'd recommend UTF8-CPP for that (used it in work projects already and works perfectly fine without having to use some huge library such as Boost). 为此,我建议使用UTF8-CPP (已经在工作项目中使用了它,并且可以很好地工作,而不必使用Boost等大型库)。

如果使用Qt库,则可以使用QTextStream::setGenerateByteOrderMark(bool)添加BOM。

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

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