简体   繁体   中英

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:

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)

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. I've included fstream, so not sure why it's doing this

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

As @chris pointed out, pre-C++11, ifs.open expects a char* , not an std::string . Try ifs.open(name.c_str()) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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