我的代码:
void listall()
{
string line;
ifstream input;
input.open("registry.txt", ios::in);
if(input.is_open())
{
while(std::getline(input, line, "\n"))
{
cout<<line<<"\n";
}
}
else
{
cout<<"Error opening file.\n";
}
}
我是C ++的新手,我想逐行打印出一个文本文件。我正在使用Code :: Blocks。
它给我的错误是:
错误:没有匹配函数来调用'getline(std :: ifstream&amp;,std :: string&amp;,const char [2])'
答案 0 :(得分:4)
这些是std::getline
的有效重载:
istream& getline (istream& is, string& str, char delim);
istream& getline (istream&& is, string& str, char delim);
istream& getline (istream& is, string& str);
istream& getline (istream&& is, string& str);
我确定你的意思是std::getline(input, line, '\n')
"\n"
不是字符,它是一个大小为2的字符数组('\n'
为1,NUL终止符为'\0'
为另一个字符。)
答案 1 :(得分:1)
写下这个:
#include <fstream> // for std::ffstream
#include <iostream> // for std::cout
#include <string> // for std::string and std::getline
int main()
{
std::ifstream input("registry.txt");
for (std::string line; std::getline(input, line); )
{
std::cout << line << "\n";
}
}
如果你想要错误检查,它是这样的:
if (!input) { std::cerr << "Could not open file.\n"; }