C++ 读入文件中的中文字符

环境:C++,VS2013,32位WIN7

一、文件类型为Unicode

 

///
/// 函数功能: 读入文件内容 /// 参考:http://blog.csdn.net/xiaobai1593/article/details/7060730 /// wstring readFileIntoStringuNNICODE(const char * filename) { ifstream ifile(filename, ios::binary); wstring res; if (ifile) { wchar_t wc; while (!ifile.eof()) { ifile.read((char *)(&wc), 2); res = res + wc; } } ifile.close(); return res; }

  

二、文件类型为ANSI

 

///
/// 函数功能:读入ANSI文件中的中文字符
/// 参考:http://tieba.baidu.com/p/1865939813
///
wstring readFileIntoStringuANSI(const char * filename) {
	wifstream ifile(filename, ios::binary);
	wstring res;
	ifile.imbue(std::locale("CHS"));
	if (ifile) {
		wchar_t wc;
		while (!ifile.eof()) {
			ifile.read((&wc), 1);
			res = res + wc;
		}
	}
	ifile.close();
	return res;
}

  

三、文件类型为UTF-8

 

posted @ 2015-06-30 11:29  poemqiong  阅读(3861)  评论(0编辑  收藏  举报