有没有办法用 wchar_t* 或 wstring 文件名创建 fstream、ifstream 或 ofstream 对象? (c++)

问题描述 投票:0回答: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 

但这会导致链接错误(text

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

有谁知道如何打开具有宽字符名称的文件(这是 Windows,因此将文件名转换为 UTF-8 不适用于某些文件名)

c++ fstream msys2 wchar-t
1个回答
0
投票

<filesystem>
可以做到。

#include <filesystem>
#include <fstream>

namespace fs = std::filesystem;

int main() {
  std::ifstream file(fs::path(L"path"));
}

https://godbolt.org/z/jWrTGYo1K

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.