vendredi 2 avril 2021

Why my getline() does not read empty lines of a file?

I am writing a function to read a file and display every lines except the empty line. However, when I tried to avoid an empty line, the display still includes every empty line. What's wrong with my code?

ifstream rfile;
int lineNum{};

rfile.open(inputFilePath);
for (string line; getline(rfile, line);) {
    lineNum++;
    if (line.empty())
        cerr << "Line " << lineNum << " is empty." << '\n';
    else
        cout << lineNum << ": " << line << '\n';
}

The input file contains:



10* 8
44 - 88
12 + 132
70 / 7

There are 3 new lines. But my output is:

1: 
2: 
3: 10* 8
4: 44 - 88
5: 12 + 132
6: 70 / 7
7: 

Why the new lines are still showing?

Aucun commentaire:

Enregistrer un commentaire