【问题标题】:reading input in from a file从文件中读取输入
【发布时间】:2015-06-21 06:43:50
【问题描述】:

所以我是 C++ 的新手,我一直在互联网上寻找如何做到这一点,到目前为止我有这个:

void includeFile(string name){
    ifstream ifs;
    ifs.open(name);
    string commands;
    while (getline(ifs,commands)){
        commandReader(ifs);
    }
    ifs.close();
}

(commandReader 是一个接受 istream 的函数)

当我尝试编译时,我收到错误“调用没有匹配的函数”,然后给了我 ifs.open(name) 行的行号。我已经包含了 fstream,所以不确定它为什么这样做

【问题讨论】:

  • 以后试试reference。请注意,该函数有两个重载,其中一个在 C++11 中可用。

标签: c++ fstream


【解决方案1】:

对不起,没关系;在我发布此内容后立即找到了答案。 解决方案是使用 name.c_string() 作为参数,因为字符串支持仅在 c++11 中添加

【讨论】:

    【解决方案2】:

    正如@chris 指出的,在 C++11 之前,ifs.open 需要 char*,而不是 std::string。试试ifs.open(name.c_str())

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 2015-02-05
      • 2011-07-18
      相关资源
      最近更新 更多