0

I'm trying to get the number of lines in a txt file. This is the method I've been trying to implement, but it prints n=1.

#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;

int main(){
    int n=0;
    string line;

    ifstream fdatos;
    fdatos.open("temp_toma5.txt");
    
    while (!fdatos.eof()){
        getline(fdatos, line);
        n++;
    }

    cout<<"n = "<<n<<endl<<endl;
    fdatos.close();
return 0;
}

This is the structure of the file

0   0.03    0
5   0.03    0.01
10  0.02    0
15  0.02    0
20  0.02    0
25  0.03    0
30  0.03    0

Thank you in advanced

  • 4
    Worthwhile reading: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Feb 22 '21 at 22:06
  • Please take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Then [edit] your question to improve it. Like telling us what happens, and what should happen. – Some programmer dude Feb 22 '21 at 22:22
  • _"it's not working and I don't know why"_ does not describe a specific behavior or ask a question. At best, it is a complaint. This could mean almost anything. Does your code even compile? Does it successfully open the file? Does it output a value different from what you expect? Be specific. – paddy Feb 22 '21 at 22:50

1 Answers1

1

You should do it like this

while (getline(fdatos, line))
    ++n;

getline returns false at the end of file because it picks up an "empty line".

Full working snippet

#include <iostream>
#include <string>
#include <fstream>

int main(){

    int n = 0;
    std::string line;
    std::ifstream fdatos("temp_toma5.txt");

    while (std::getline(fdatos, line))
        ++n;

    std::cout << "n = " << n << "\n\n";

    fdatos.close();
    return 0;
}

gives the right answer n = 7.

The comment of user4581301 points to the explanation.

tboschi
  • 124
  • 11
  • 2
    I need to split some hairs here... `getline` does not _"return false at the end of the file"_. It returns a stream reference, always. When you use the return value this way, the stream is implicitly cast to bool, invoking the bool operator overload for streams which tests if the `fail` bit is set. That indicates that a read failed which may be the result of end-of-file _and_ no characters read, or the stream being closed, or some other I/O error. In this case, the operator returns false. If no failure occurred, it returns true. – paddy Feb 22 '21 at 22:58
  • That's right, it returns a reference to the input stream, for which the standard has a clear definition for bool casting https://en.cppreference.com/w/cpp/io/basic_ios/operator_bool Apologies for my sloppiness. – tboschi Feb 22 '21 at 23:21
  • 1
    I did not hammer the question shut as a duplicate because we don't know for certain that this is the bug the asker is hunting. It's definitely a bug, but for all we know they aren't successfully opening the file because temp_toma5.txt" isn't in the working directory – user4581301 Feb 22 '21 at 23:30