简体   繁体   English

从文件中读取输入

[英]reading input in from a file

so I'm a newbie at C++, and I've been poking around on the internet on how to do this, and so far I have this: 所以我是C ++的新手,而且我一直在互联网上闲逛着如何做到这一点,到目前为止,我有:

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

(commandReader is a function that takes an istream) (commandReader是采用istream的函数)

When I try to compile, I get the error "no matching function for call" and then gives me the line number for the ifs.open(name) line. 当我尝试编译时,出现错误“没有匹配的调用函数”,然后为我提供了ifs.open(name)行的行号。 I've included fstream, so not sure why it's doing this 我已经包含了fstream,所以不确定为什么要这样做

Sorry, never mind; 对不起,没关系; found the answer right after I posted this. 我在发布此信息后立即找到了答案。 The solution was to have name.c_string() as the parameter instead, as string support was only added in c++11 解决方案是使用name.c_string()作为参数,因为仅在c ++ 11中添加了字符串支持

As @chris pointed out, pre-C++11, ifs.open expects a char* , not an std::string . 正如@chris指出的那样,在C ++ 11之前, ifs.open需要一个char* ,而不是std::string Try ifs.open(name.c_str()) . 尝试ifs.open(name.c_str())

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

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