繁体   English   中英

从文件中读取输入

[英]reading input in from a file

所以我是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,所以不确定为什么要这样做

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM