小编Chr*_*ner的帖子

求istreambuf_iterator <wchar_t>澄清,阅读Unicode字符的完整文本文件

在Scott Meyers的"Effective STL"一书中,有一个将整个文本文件读入std :: string对象的好例子:

std::string sData; 

/*** Open the file for reading, binary mode ***/
std::ifstream ifFile (“MyFile.txt”, std::ios_base::binary); // Open for input, binary mode

/*** Read in all the data from the file into one string object ***/
sData.assign (std::istreambuf_iterator <char> (ifFile),
              std::istreambuf_iterator <char> ());
Run Code Online (Sandbox Code Playgroud)

请注意,它以8字节字符的形式读取.这非常有效.最近虽然我需要读取包含Unicode文本的文件(即每个字符两个字节).但是,当我尝试(天真地)更改它以将数据从Unicode文本文件读取到std :: wstring对象时,如下所示:

std::wstring wsData; 

/*** Open the file for reading, binary mode ***/
std::wifstream ifFile (“MyFile.txt”, std::ios_base::binary); // Open for input, binary mode

/*** Read in all the data from the file …
Run Code Online (Sandbox Code Playgroud)

c++ unicode istream-iterator wstring wifstream

11
推荐指数
1
解决办法
2544
查看次数

标签 统计

c++ ×1

istream-iterator ×1

unicode ×1

wifstream ×1

wstring ×1