• Register
1 vote
34 views

Problem:

I am a new student of c++ and learning new things day by day.

Well, I have a text file containing thousands of lines possibly. I am willing to give them a count (if possible). What is the easiest and efficient way to get number of lines in a file c++? Please, keep it simple so that I can understand the code easily.
Thanks.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

The line counting program is very useful in different fields. Please, find the code snippet below.

#include <iostream>
#include <fstream>
using namespace std;

int main() {
 ifstream f;
   char ch;
   int numberchars, numberlines;
   f.open("Test.txt");
   numberchars = 0;
   numberlines = 0;
   f.get(ch);
   while (f) {
     while (f && ch != '\n') {
       numberchars = numberchars + 1;
       f.get(ch);
     }
     numberlines = numberlines + 1;
     f.get(ch);
   }
   cout << "The file has " << numberlines << " lines and " 
     << numberchars << " characters" << endl;
   return(0);
}

Note: I’ve added the code to find the characters too in this program.

13 9 6
94,240 points

Related questions

1 vote
1 answer 25 views
25 views
Problem: I have a python file that contains too many lines of code. I have written a code to count the total number of lines in a exact file. My code: def file_name(fname): with open(fname) as fn: for i, l in enumerate(fn): pass return i+1 Does the code give me the exact count ? If not how can I solve this?
asked Apr 5, 2020 LizzyM 6.1k points
1 vote
1 answer 17 views
17 views
Problem: Well, I have a text file containing thousands of lines possibly. I am willing to give them a count (if possible). What is the easiest and efficient way to write a program to get number of lines in a file python? Please, keep it simple so that I can understand the code easily. Thanks.
asked Mar 29, 2020 Gavin 15.3k points
0 votes
1 answer 1 view
1 view
Problem: I use huge data files, sometimes I only need to know the number of lines in these files, usually I open them up and read them line by line until I reach the end of the file I was wondering if there is a smarter way to do that
asked 2 days ago Ali 4.4k points
0 votes
1 answer 16 views
16 views
Problem: for reasonably sized files, is to read the file as a list of lines so that the count of lines is the length of the list. If the file&rsquo;s path is in a string bound to the thefilepath variable, that&rsquo;s just: count = len(open(thefilepath).readlines ... humongous files, a loop using the xreadlines method always works: count = 0 for line in open(thefilepath).xreadlines( ): count += 1
asked Jan 23 Mashhoodch 9.1k points
0 votes
1 answer 9 views
9 views
Problem: I need to get a line count of a large file (hundreds of thousands of lines) in python. What is the most efficient way both memory- and time-wise? At the moment I do: def file_len(fname): with open(fname) as f: for i, l in enumerate(f): pass return i + 1 is it possible to do any better?
asked 5 days ago Mashhoodch 9.1k points
0 votes
1 answer 7 views
7 views
Problem: lines() - read all lines of the file as a stream count() - returns the number of elements in the stream Here, if the file input.txt contains the following content: This is the question on Java Examples. The examples count number of lines in a file. , youhave used the java.nio.file package.
asked 5 days ago Ali 4.4k points
0 votes
1 answer 23 views
23 views
Problem Hi, How can I get to know number of lines in a file on Linux?
asked Oct 31, 2020 hashq 1.1k points
0 votes
1 answer 39 views
39 views
I want to count lines, words and characters from a file. I'm having a problem counting the right amount of characters and words.
asked Oct 20, 2020 Ahsan 1.8k points
0 votes
1 answer 31 views
31 views
Problem: I have recently started learning the C Programming. Currently I am trying to write C programs. In this coding problem I am trying to count the number of characters, newlines and number of words. But I think there is some issue in the second while loop which I am unable to ... ();                 nc++;             }             nc++;         }     }     printf("%d %d %d",nl,nc,nw); }
asked Jun 16, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 4 views