从文件中读取输入
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 的函数)
当我尝试编译时,出现错误 "no matching function for call",然后给我 ifs.open(name) 行的行号。我包含了 fstream,所以不确定为什么要这样做
抱歉,没关系;我发布后立即找到了答案。
解决方案是将 name.c_string() 作为参数,因为仅在 c++11
中添加了字符串支持
正如@chris 指出的那样,在 C++11 之前,ifs.open
期望 char*
,而不是 std::string
。试试 ifs.open(name.c_str())
.
所以我是 C++ 的新手,我一直在互联网上寻找如何做到这一点,到目前为止我有这个:
void includeFile(string name){
ifstream ifs;
ifs.open(name);
string commands;
while (getline(ifs,commands)){
commandReader(ifs);
}
ifs.close();
}
(commandReader 是一个接受 istream 的函数)
当我尝试编译时,出现错误 "no matching function for call",然后给我 ifs.open(name) 行的行号。我包含了 fstream,所以不确定为什么要这样做
抱歉,没关系;我发布后立即找到了答案。 解决方案是将 name.c_string() 作为参数,因为仅在 c++11
中添加了字符串支持正如@chris 指出的那样,在 C++11 之前,ifs.open
期望 char*
,而不是 std::string
。试试 ifs.open(name.c_str())
.