【发布时间】:2019-04-08 23:07:38
【问题描述】:
我在 ASCII 文件上使用了以下内容:
#include <fstream>
#include <streambuf>
#include <string>
#include <cerrno>
std::string get_file_contents(const char *filename)
{
std::ifstream in(filename, std::ios::in | std::ios::binary);
if (in)
{
return(std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()));
}
throw(errno);
}
我想确认它是否适用于 UTF-8 文件以及 std::string 或是否有任何特殊设置?
【问题讨论】:
-
std::string更多的是字节字符串,而不是 UTF-8 编码单元的字符串。应该可以正常工作。 -
一个字符串将存储您喜欢的任何编码。棘手的部分是一旦它在那里,你如何处理它。
-
全部为真,但某些字符串函数使用包含字符编码的语言环境。而且,如果您有一些带有一种字符编码的字符串和一些带有另一种字符编码的字符串,那么祝您好运。也许您有更具体的问题。
-
相关:What is the best way to read an entire file into a std::string in C++? 有很多不同的方法可以解决这个问题。