Reputation: 531
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
Upvotes: 7
Views: 68537
Reputation: 51
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;
}
Upvotes: -1
Reputation: 11
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);
Upvotes: 1
Reputation: 74008
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;
Upvotes: 10
Reputation: 7064
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;
Upvotes: 0
Reputation: 47784
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');
Upvotes: 14
Reputation: 13984
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;
Upvotes: 2
Reputation: 409136
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".
Upvotes: 1