gpt4 book ai didi

c++ - std::wofstream::open 不适用于 MAC/Xcode

转载 作者:行者123 更新时间:2023-11-28 05:48:28 24 4
gpt4 key购买 nike

我正在使用宽字符串文件流 std::wofstream 打开文件并读取其内容。我使用了 header fstream。但是当我在 XCode 7 上编译这段代码时,它显示以下错误

No matching member function for call to 'open'

我的代码是这样的

header used <fstream>

std::wofstream out;
out.open(filename, std::ios::binary); <--- error
* filename is wide char string

注意:它在 Visual Studio 2015 的 Windows 上运行良好。

最佳答案

std::wofstream 只是一个模板类型为 wchar_tstd::basic_ofstreamstd::basic_ofstream::open根据标准有两个重载

void open( const char *filename,
ios_base::openmode mode = ios_base::out );
void open( const std::string &filename,
ios_base::openmode mode = ios_base::out );

如您所见,wchar_t*std::wstring 都没有。我怀疑 MSVS 添加了一个重载以适应使用 open 和 xcode 没有的宽字符串。

std::stringconst char* 传递给 open()

应该没有问题

我想指出,没有理由构造对象然后调用open()。如果你想构建并打开一个文件,那么只需使用将执行此操作的构造函数。

std::wofstream out;
out.open(filename, std::ios::binary);

成为

std::wofstream out(filename, std::ios::binary);

关于c++ - std::wofstream::open 不适用于 MAC/Xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35773613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com