getline() 读取额外的一行

发布于 2024-12-07 08:32:36 字数 333 浏览 5 评论 0原文

ifstream file("file.txt");
 if(file.fail())
{
cout<<"Could not open the file";
exit(1);
}
else
{
      while(file)
      {
        file.getline(line[l],80); 
                          cout<<line[l++]<<"\n";
      } 
}

我使用二维字符数组来保留从文件中读取的文本(多于一行),以计算文件中的行数和单词数,但问题是 getline 总是读取额外的行。

ifstream file("file.txt");
 if(file.fail())
{
cout<<"Could not open the file";
exit(1);
}
else
{
      while(file)
      {
        file.getline(line[l],80); 
                          cout<<line[l++]<<"\n";
      } 
}

I am using a two dimensional character array to keep the text (more than one line) read from a file to count the number of lines and words in the file but the problem is that getline always reads an extra line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

┾廆蒐ゝ 2024-12-14 08:32:36

我正在编写的代码:

ifstream file("file.txt");
 if(file.fail())
{
cout<<"Could not open the file";
exit(1);
}
else
{
      while(file)
      {
        file.getline(line[l],80); 
        cout<<line[l++]<<"\n";
      } 
}

第一次 getline 失败时,您仍然会增加行计数器并输出(不存在的)行。

始终检查是否有错误。

额外建议:使用 标头中的 std::string ,并使用其 getline 函数。

干杯&嗯。

Your code as I'm writing this:

ifstream file("file.txt");
 if(file.fail())
{
cout<<"Could not open the file";
exit(1);
}
else
{
      while(file)
      {
        file.getline(line[l],80); 
        cout<<line[l++]<<"\n";
      } 
}

The first time getline fails, you still increment the line counter and output the (non-existing) line.

Always check for an error.

extra advice: use std::string from the <string> header, and use its getline function.

cheers & hth.

看轻我的陪伴 2024-12-14 08:32:36

问题是,当您到达文件末尾时,对 file 的测试仍然会成功,因为您尚未读到文件末尾。因此,您还需要测试 getline() 的返回。

由于您需要测试 getline() 的返回值以查看是否成功,因此您也可以将其放在 while 循环中:

while (file.getline(line[l], 80))
    cout << line[l++] << "\n";

这样您就不需要对 进行单独的测试文件getline()

The problem is when you're at the end of the file the test on file will still succeed because you have not yet read past the end of file. So you need to test the return from getline() as well.

Since you need to test the return from getline() to see if it succeeded, you may as well put it right in the while loop:

while (file.getline(line[l], 80))
    cout << line[l++] << "\n";

This way you don't need a separate test on file and getline().

眼中杀气 2024-12-14 08:32:36

这将解决您的问题:

ifstream file("file.txt");
if(!file.good())
{
  cout<<"Could not open the file";
  exit(1);
}
else
{
  while(file)
  {
    file.getline(line[l],80);
       if(!file.eof())
          cout<<line[l++]<<"\n";
  } 
}

它更强大

This will solve your problem:

ifstream file("file.txt");
if(!file.good())
{
  cout<<"Could not open the file";
  exit(1);
}
else
{
  while(file)
  {
    file.getline(line[l],80);
       if(!file.eof())
          cout<<line[l++]<<"\n";
  } 
}

Its more robust

悍妇囚夫 2024-12-14 08:32:36

仅当 file.good() 为 true 时才执行 cout。您看到的额外行来自最后一次调用 file.getline() ,该调用读取了文件末尾。

Only do the cout if file.good() is true. The extra line you're seeing comes from the last call to file.getline() which reads past the end of the file.

像极了他 2024-12-14 08:32:36

文件是否以换行符结尾?如果是这样,则直到经过一个额外的循环后才会触发 EOF 标志。例如,如果文件是

abc\n
def\n

那么循环将运行3次,第一次将得到abc,第二次将得到def,第三次将得到def将一无所获。这可能就是您看到额外一行的原因。

尝试在 getline 之后检查流上的失败位。

Does the file end with a newline? If it does, the EOF flag will not be triggered until one extra loop passes. For example, if the file is

abc\n
def\n

Then the loop will be run 3 times, the first time it will get abc, the second time it will get def and the third time it will get nothing. That's probably why you see an additional line.

Try checking the failbit on the stream AFTER the getline.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文