且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将 std::string 写入 UTF-8 文本文件

更新时间:2023-11-14 16:28:58

UTF-8 影响 std::string 的唯一方式是 size(), length(),所有索引都以字节为单位,而不是字符.

The only way UTF-8 affects std::string is that size(), length(), and all the indices are measured in bytes, not characters.

而且,正如 sbi 指出的那样,递增 std::string 提供的迭代器将按字节而不是按字符向前推进,因此它实际上可以指向多字节 UTF-8 的中间代码点.标准库中没有提供支持 UTF-8 的迭代器,但在网络"上提供了一些.

And, as sbi points out, incrementing the iterator provided by std::string will step forward by byte, not by character, so it can actually point into the middle of a multibyte UTF-8 codepoint. There's no UTF-8-aware iterator provided in the standard library, but there are a few available on the 'Net.

如果你记得的话,你可以将 UTF-8 放入 std::string,以通常的方式将其写入文件等(我的意思是你的方式)使用 std::string 里面没有 UTF-8).

If you remember that, you can put UTF-8 into std::string, write it to a file, etc. all in the usual way (by which I mean the way you'd use a std::string without UTF-8 inside).

您可能希望以字节顺序标记开始您的文件,以便其他程序知道它是 UTF-8.

You may want to start your file with a byte order mark so that other programs will know it is UTF-8.