6

Is there a function I can use to get total file line number in C++, or does it have to be manually done by for loop?

#include <iostream>
#include <ifstream>

ifstream aFile ("text.txt");
if (aFile.good()) {
//how do i get total file line number?

}

text.txt

line1
line2
line3
Aruna
  • 11,959
  • 3
  • 28
  • 42
NewFile
  • 501
  • 5
  • 10
  • 16

7 Answers7

13

I'd do like this :

   ifstream aFile ("text.txt");   
   std::size_t lines_count =0;
   std::string line;
   while (std::getline(aFile , line))
        ++lines_count;

Or simply,

  #include<algorithm>
  #include<iterator>
  //...
  lines_count=std::count(std::istreambuf_iterator<char>(aFile), 
             std::istreambuf_iterator<char>(), '\n');
P0W
  • 46,614
  • 9
  • 72
  • 119
10

There is no such function. Counting can be done by reading whole lines

std::ifstream f("text.txt");
std::string line;
long i;
for (i = 0; std::getline(f, line); ++i)
    ;

A note about scope, variable i must be outside for, if you want to access it after the loop.


You may also read character-wise and check for linefeeds

std::ifstream f("text.txt");
char c;
long i = 0;
while (f.get(c))
    if (c == '\n')
        ++i;
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
2

I fear you need to write it by yourself like this:

int number_of_lines = 0;
 std::string line;
 while (std::getline(myfile, line))
        ++number_of_lines;

 std::cout << "Number of lines in text file: " << number_of_lines;
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
1

Have a counter, initialized to zero. Read the lines, one by one, while increasing the counter (the actual contents of the line is not interesting and can be discarded). When done, and there was no error, the counter is the number of lines.

Or you can read all of the file into memory, and count the newlines in the big blob of text "data".

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Solutions in https://stackoverflow.com/a/19140230/9564035 are good but not provide same output. If you want to count lines only ended with '\n' use

#include<algorithm>
#include<iterator>
//...
    ifstream aFile ("text.txt");
    lines_count=std::count(std::istreambuf_iterator<char>(File), 
    std::istreambuf_iterator<char>(), '\n');

If you want to count also line that not ended by '\n' (last line) you should use getLine solution

ifstream aFile ("text.txt");
std::size_t lines_count =0;
std::string line;
while (std::getline(aFile , line))
   ++lines_count;

Note that if you previously read from file you should set pointer to beginning of file for file.seekg(std::ios_base::beg);

Makox
  • 11
  • 2
0

Fast way then above solutions like P0W one save 3-4 seconds per 100mb

std::ifstream myfile("example.txt");

// new lines will be skipped unless we stop it from happening:    
myfile.unsetf(std::ios_base::skipws);

// count the newlines with an algorithm specialized for counting:
unsigned line_count = std::count(
    std::istream_iterator<char>(myfile),
    std::istream_iterator<char>(), 
    '\n');

std::cout << "Lines: " << line_count << "\n";
return 0;
Yash
  • 6,644
  • 4
  • 36
  • 26
  • Using istreambuf_iterator instead of istream_iterator makes it 5 times faster in my experiments. – gus3001 Mar 06 '15 at 11:03
-1

Just copy this & run.

#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    fstream file;
    string filename = "sample input.txt";
    file.open(filename.c_str()); //read file as string 
    int lineNum = 0;
    string s;
    if (file.is_open())
    {
        while (file.good())
        {
            getline(file, s);
            lineNum++;
            cout << "The length of line number " << lineNum << " is: " << s.length() << endl;
        }
        cout << "Total Line : " << lineNum << endl;
        file.close();
    }

    return 0;
}
Mehdi Mostafavi
  • 880
  • 1
  • 12
  • 25
  • Explicitly asking for upvotes is against SO policy and your post has been edited to remove that part. However, you're unlikely to get any upvotes on an answer that recommends using `#include `. – Adrian Mole Apr 14 '21 at 23:20
  • I didn't know that SO policy. Thanks for your valuable feedback. – Abdullah Al Masum Apr 15 '21 at 05:27