在利用STL读取文件时,对于中文路径不能识别.参考以下两个位置文档资料解决:
1.解决获取文件大小和文件内容的问题
http://www.cplusplus.com/reference/iostream/istream/tellg.html
该文档给出了文件读取,及取文件大小的方法,但是对于中文目录却会产生问题,(--批注:此例子对于小文件是适用的,
对于大的文件需要循环读取,或利用heap去处理)
// read a file into memory #include <iostream> #include <fstream> using namespace std; int main () { int length; char * buffer; ifstream is; is.open ("test.txt", ios::binary ); // get length of file: is.seekg (0, ios::end); length = is.tellg(); is.seekg (0, ios::beg); // allocate memory: buffer = new char [length]; // read data as a block: is.read (buffer,length); is.close(); cout.write (buffer,length); return 0; } |
2.解决中文路径文件不能读取问题
参考http://www.cppblog.com/mythma/archive/2006/06/09/8349.aspx
问题:
有时候用ifstream或ofstream打开带有中文路径的文件会失败。
解决办法(两种):
1、使用C语言的函数设置为中文运行环境
setlocale(LC_ALL,"Chinese-simplified");
2、使用STL函数设置为系统语言环境
std::locale::global(std::locale(""));
在文件中增加 std::locale::global(std::local(""));即可