代码之家  ›  专栏  ›  技术社区  ›  Stas Badzi

是否有方法使用wchar_t*或wstring文件名创建fstream、ifstream或ofstream对象?c

  •  1
  • Stas Badzi  · 技术社区  · 1 年前

    我正试图用msys2-gcc(msys2-msys2不是msys2-mingw)编译一些为msvc编译器编写的代码,它包含

    wstring filename;
    ifstream file(filename);
    

    我试图找到一种方法来使用_wfopen(),我可以从msvcrt.dll导入它,但我找不到一种方法覆盖包含file的底层__basic_file类型*

    我也试着定义

    #define _GLIBCXX_HAVE__WFOPEN 1
    #define _GLIBCXX_USE_WCHAR_T 1 
    

    但这会导致联动错误( here )

    我还尝试从mingw-w64gcc导出函数create_ifstream(const wchar_t*),但ifstream没有复制构造函数,因此它要么导致编译器错误,要么在运行时导致分段错误

    有人知道我如何打开一个宽字符名的文件吗(这是windows,所以将文件名转换为UTF-8对某些文件名不起作用)

    1 回复  |  直到 1 年前
        1
  •  4
  •   3CxEZiVlQ    1 年前

    <filesystem> 可以做到这一点。

    #include <filesystem>
    #include <fstream>
    #include <string>
    
    namespace fs = std::filesystem;
    
    int main() {
      std::wstring some_path;
      std::ifstream file1(fs::path(some_path));
      std::ifstream file2(fs::path(L"some_path"));
    }
    

    https://godbolt.org/z/h14eGrErz -gcc和msvc。