C++ fstream未打开路径名中带有重音符号的文件
但这不是我想要的:如果可能的话,我想将failbit设置为正常C++ fstream未打开路径名中带有重音符号的文件,c++,exception,visual-studio-2012,fstream,C++,Exception,Visual Studio 2012,Fstream,但这不是我想要的:如果可能的话,我想将failbit设置为正常 我正在使用Visual Studio 2012和Windows 8。您确定可以使用窄字符串吗?源文件编码是什么?很可能与文件系统使用的Windows编码不匹配。@在posix/linux中允许使用chris。UTF-8@njr,是的,但它是Windows 8:p这就解释了,因为根据这个答案:Windows文件名(在NTFS上)存储为UTF-16,因此需要使用wchar\u t,因为ANSI版本的Windows函数不理解UTF-8。
我正在使用Visual Studio 2012和Windows 8。您确定可以使用窄字符串吗?源文件编码是什么?很可能与文件系统使用的Windows编码不匹配。@在posix/linux中允许使用chris。UTF-8@njr,是的,但它是Windows 8:p这就解释了,因为根据这个答案:Windows文件名(在NTFS上)存储为UTF-16,因此需要使用wchar\u t,因为ANSI版本的Windows函数不理解UTF-8。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char **argv) {
try
{
ifstream file;
string simplePath = "I:/foo.conf";
string markPath = "I:/Folder_à/foo.conf";
file.exceptions(ifstream::failbit | ifstream::badbit | ifstream::eofbit);
file.open(simplePath.c_str()); // ok
file.open(markPath.c_str()); // exception ios_base::failbit set
}
catch (ifstream::failure f)
{
cout << "Exception " << f.what() << endl;
return 1;
}
return 0;
}
file.exceptions(ifstream::badbit | ifstream::eofbit); // removed failbit from the mask