【问题标题】:FStream class and using strings as parametersFStream 类并使用字符串作为参数
【发布时间】:2012-03-13 21:55:25
【问题描述】:
Deneme::Deneme(string FileName){

 fstream textfile;
 textfile.open(FileName);
 }

这给了我一个错误,但是当我输入 textfile.open("randomname");而不是 textfile.open(FileName);似乎没有问题。为什么是这样?这可能是一个简单的问题,但我是初学者,找不到解决方案。

【问题讨论】:

标签: c++ string text parameters fstream


【解决方案1】:

fstreams 只接受const char*。请改用textfile.open(FileName.c_str());fstream textfile(FileName.c_str());(尽管C++11 接受const std::string&)。 这是一个handy site,用于查看构造函数和函数是如何声明的。

【讨论】:

    【解决方案2】:

    fstream 的 open 方法在您按值传递 std::string 时采用 const char 指针,我想这可能是错误。试试:

    textfile.open(FileName.c_str());
    

    【讨论】:

      【解决方案3】:

      使用符合标准 C++ 的系统:C++2011 提供构造函数和采用 std::string const&open() 函数。对于 C++2011 之前的系统,您需要使用 name.c_str() 传递给文件流。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-10
        • 2019-11-01
        • 1970-01-01
        • 2011-12-11
        • 2013-01-18
        • 1970-01-01
        相关资源
        最近更新 更多