[英]getline can't get the first line of the file
嗨,我是新来的,需要我的协助才能完成任务。 我想从文件中读取数据。 我使用过getline,但它不会读取文件中的第一行,有人可以帮我吗? 这是我的代码:
void test(std::ifstream& infile, string& a, string& b, string& c)
{
infile >> a;
infile >> b;
infile >> c;
}
int main()
{
ifstream file_("Level1.txt");
string line;
string a, b, c;
while (getline(file_, line))
{
test(file_, a, b, c);
}
cout << a << " " << b << " " << c;
return 0;
}
在我的文件中:
aa
bb
cc
但输出:
bb cc
Getline
正在获取当前行,因此您的字符串如下所示
a = "bb"
b = "cc"
c = ""
因为您已经阅读了第一行并且存储在第一line
。您可以仅检查文件是否打开,然后调用test函数,因为它将从文件的开头开始,而不会跳过第一行。
if(file_.is_open())
{
test(file_, a, b, c);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.