fstream not opening files with accent marks in pathname

北城以北 提交于 2020-01-06 07:24:51

问题


#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;
}

If a file has an accent mark (e.g. à) within its pathname, the open() function throwns a ios_base::failbit set exception.

A simple fix is

file.exceptions(ifstream::badbit | ifstream::eofbit); // removed failbit from the mask

but is not what I want: if possible I'd like to set the failbit as normal.

I'm using Visual Studio 2012 and Windows 8.

来源:https://stackoverflow.com/questions/14483926/fstream-not-opening-files-with-accent-marks-in-pathname

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!