简单任务:我想读取一个文件名不是 ascii 的文件。
在 linux 和 MacOS 上,我只是将文件名作为 UTF-8 编码字符串传递给 fstream
构造函数。在 Windows 上,这会失败。
正如我从 this question 中了解到的那样, windows 根本不支持 utf-8 文件名。但是,它提供了一个自己的非标准 open
方法,该方法采用 utf-16 wchar_t*
。因此,我可以简单地将我的 string
转换为 utf-16 wstring
并且没问题。然而,在 MinGW 标准库中,fstream
的 wchar_t* open
方法根本不存在。
那么,如何在 MinGW 上打开一个非 ascii 文件名?
最佳答案
我之前遇到过同样的问题。不幸的是,在您可以使用 std::filesystem::path
之前,您需要以某种方式解决这个问题,例如通过包装所有东西,例如像我一样here ,这使得“用户代码”看起来像这样:
auto stream_ptr = open_ifstream(file_name); // I used UTF-8 and converted to UTF-16 on Windows as in the code linked above
auto& stream = *stream_ptr;
if(!stream)
throw error("Failed to open file: \'" + filename + "\'.");
丑陋的是的,有点便携,是的。请注意,这不适用于 Windows 上的 Libc++,尽管该组合目前无论如何都不起作用,但这无关紧要。
关于c++ - MinGW C++ : Reading a file with non-ascii file name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39594492/