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

Nit*_*hin 3 c++ byte-order-mark file-handling

我正在尝试使用 C++ 创建带有 BOM 的 UTF8 的 csv 文件。谁能帮我,该怎么做?

Mar*_*rio 5

只需将 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
Run Code Online (Sandbox Code Playgroud)

然后,您只需确保您实际上编写的是正确的 UTF-8,而不是 ANSI 或宽字符字符串。我建议使用UTF8-CPP(已经在工作项目中使用它并且工作得很好,而无需使用一些巨大的库,例如 Boost)。