【问题标题】:sourceFile.open(filepath); [duplicate]sourceFile.open(文件路径); [复制]
【发布时间】:2012-10-23 04:40:23
【问题描述】:

可能重复:
C++ type of argument to ifstream::open()
C++ ifstream error using string as opening file path.

怎么了

ifstream sourceFile;
sourceFile.open(filepath);

我得到了一些关于:

没有对ifstream::open(string&)的匹配函数调用

怎么了?

相同
ifstream sourceFile(filepath)

filepath 是一个字符串

【问题讨论】:

    标签: c++ io


    【解决方案1】:

    从 C++11 开始,iostreams 可以将string 作为参数来命名要打开的文件——但这是在 C++11 中添加的,所以很多库还没有包含它。更新到最新版本的编译器/库可能会有所帮助(但话又说回来,它可能不会——我怀疑这种支持是否相当普遍)。

    【讨论】:

      【解决方案2】:

      看起来filepathstringOpen 只接受原始指针。 所以这样做 - Open(filepath.c_str());

      【讨论】:

        【解决方案3】:

        它需要 char* filename 而不是 字符串 作为参数。

        sourceFile.open(filepath.c_str());
        

        The Function is std::ifstream::open
            void open ( const char * filename, ios_base::openmode mode = ios_base::in );
        

        (Reference)

        【讨论】:

          猜你喜欢
          • 2013-01-31
          • 2014-07-25
          • 1970-01-01
          • 2022-10-17
          • 1970-01-01
          • 2014-08-16
          • 1970-01-01
          • 1970-01-01
          • 2016-08-31
          相关资源
          最近更新 更多