【问题标题】:One extra line being read in file handling在文件处理中读取了额外的一行
【发布时间】:2021-06-22 15:59:37
【问题描述】:

我正在尝试从 C++ 中的文本文件中获取行数和单词数。但是编译器正在读取额外的一行。

#include <iostream>
#include<fstream>
using namespace std;

int main(void)
{
    ifstream f;
    string name;
    char a;
    int line, words;
    line = 0;
    words = 0;


    f.open("file.txt");

    while (f) {
        f.get(a);


        if (a == '\n')
        {
            line++;
            words++;
        }

        if (a == ' ')
        {
            words++;
        }

    }
    f.close();
    cout << "Number of words in file : " << words << endl;
    cout << "Numbers of lines in the file : " << line << endl;
}

输出:-

Number of words in file : 79
Numbers of lines in the file : 3

file.txt:-

This C++ Program which counts the number of lines in a file. The program creates an input file stream, reads a line on every iteration of a while loop, increments the count variable and the count variable is printed on the screen.
Here is source code of the C++ program which counts the number of lines in a file. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

我很困惑为什么要多读一行。请帮忙。

【问题讨论】:

    标签: c++ file-handling


    【解决方案1】:

    您没有检查f.get() 是成功还是失败。当它确实失败时,a 不会更新,并且您还没有打破循环,因此您最终会再次对aprevious 值进行操作。然后 next 循环迭代检测到失败并中断循环。

    改变这个:

    while (f) {
        f.get(a);
        ...
    }
    

    改为:

    while (f.get(a)) {
        ...
    }
    

    话虽如此,您也没有考虑到文件中的最后一行可能不以'\n' 结尾,如果不是,那么您不算该行。此外,您假设每行始终至少包含 1 个单词,因为您在每个 '\n' 上递增 words,即使对于其中没有单词的行也是如此。

    我建议使用std::getline() 读取和计算行数,使用std::istringstream 读取和计算每行中的单词,例如:

    #include <fstream>
    #include <sstream>
    #include <string>
    #include <cctype>
    using namespace std;
    
    int main(void)
    {
        ifstream f;
        string line, word;
        int lines = 0, words = 0;
    
        f.open("file.txt");
    
        while (getline(f, line))
        {
            ++lines;
            std::istringstream iss(line);
            while (iss >> word) {
                ++words;
            }
        }
    
        f.close();
    
        cout << "Number of words in file : " << words << endl;
        cout << "Numbers of lines in the file : " << line << endl;
    }
    

    【讨论】:

    • 所以 f.get(a) 在最后一次迭代中失败了,因为它失败了,所以它的值保持不变?
    • @RishabhGupta 是的。每std::basic_istream&lt;CharT,Traits&gt;::get:“读取一个字符并将其存储到ch(如果有)。否则,保持ch不变并设置failbiteofbit "
    【解决方案2】:

    这是因为您没有检查f.get(a) 返回的流是什么状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      相关资源
      最近更新 更多