count the number of lines.

Posted on 2003-03-30
Medium Priority
Last Modified: 2008-02-01
hi everyone,

i was just wondering if you could give me an idea on how to count the lines say for example a text file that contains a number of lines?

i have been running this in my code but i dont know if i am in the right track.

ifstream file("file.txt");
int count = 0;
char line[80];

if(count > 0)
cout << "the number of lines is: " << count << endl;
return 0;

now running this code always gives me a total of 1. can you tell me where the problem is? i'm a begginer of c++.
Question by:Tumbi
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

SIC_Michael earned 150 total points
ID: 8233254
Your problem is the conditional statement of your while() loop.

original: while(file.eof())
should be: while(!file.eof())

file.eof() will return 'true' if the file handle is at the end of the file; 'false' when it is not at the end.  So knowing this, you want the loop to continue if the file handle is not at the end of the associated file... and to do this you need to test for !file.eof().  

(remember, it will return 'false' when we want it to continue through the while() loop... but while() loops require that the condition be met if it is to continue, so to do this we add the 'not' to make it fit our implementation.)

Expert Comment

ID: 8233268
My friend did something rather funny when he got assigned the same problem. It was a UNIX console so he called the wc command and passed the output to a file, read the first number then removed the file. Kinda like wc a.txt > wc.txt.
We had a big laugh when the teacher decided not to call that *cheating* =)

Author Comment

ID: 8233284
sorry about that i forgot to put the !file.oef() in the question, but i did that in my code just for got to place it in my question.

Expert Comment

ID: 8233301
Actually... I'm just noticing this... you need to add another closing curly bracket after "++count;" as well.

Because, you are returning a value for the function before you have a chance to make another itteration in the while() loop.


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question