【问题标题】:std::wofstream::open is not working on MAC/Xcodestd::wofstream::open 在 MAC/Xcode 上不起作用
【发布时间】:2016-03-03 21:45:37
【问题描述】:

我正在使用宽字符串文件流 std::wofstream 打开文件并读取其内容。我使用了标头 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 上运行良好。

【问题讨论】:

    标签: c++ xcode macos osx-yosemite


    【解决方案1】:

    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 添加了一个重载以适应在 xcode 没有的宽字符串中使用 open

    std::stringconst char* 传递给open() 应该没有问题

    我想指出的是,没有理由构造对象然后调用open()。如果你想构造并打开一个文件,那么只需使用构造函数即可。

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

    变成

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

    【讨论】:

    • 那么,我应该将文件名转换为 std::string 吗?
    • @Jai 如果您将其作为std::wstring,那么可以。你可以在这里看到如何做到这一点:stackoverflow.com/questions/4804298/…
    猜你喜欢
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    相关资源
    最近更新 更多