2023年5月15日 
开发小记 如何解决【wfstream+wstring+中文文件】的乱码问题
不多说,先上一段可复现代码:
int main() {
	vector<wstring> FileContent;
	wstring FilePath = L"C:\\a.txt";
	// a.txt:中文内容,UTF-8编码
	wfstream fs(FilePath);
	wstring Line = L"";
	if (!fs.is_open()) {return 1;}
	else {
		wstring Line = L"";
		while (getline(fs, Line)) {FileContent.push_back(Line); } }
	fs.close();}让我们编译、链接、运行,程序轻松跑起来,然后呢?



这、这不对吧?今天这又是哪个字符编码要陷害我?
怎么解决呢?
在读取文件之前加上这个:
        fs.imbue(::std::locale("zh_CN.UTF-8"));注意,std的命名空间是有必要的,因为你有可能遇到其他库有同名函数,比如Qt。


这两种都是可以的,第一种是匿名变量,少占用个名字,其他没区别。