Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the easiest way to count the newlines in an ASCII file?

Tags:

c

Which is the fastest way to get the lines of an ASCII file?

like image 579
Sunscreen Avatar asked Nov 25 '10 15:11

Sunscreen


People also ask

How do I count the number of lines in a file?

Using “wc -l” There are several ways to count lines in a file. But one of the easiest and widely used way is to use “wc -l”. The wc utility displays the number of lines, words, and bytes contained in each input file, or standard input (if no file is specified) to the standard output. 1.

How do I count the number of lines in a file in C++?

How To Count Lines In A File In C++? To count the lines in a file in c++, we will open the file in the read mode only as there are no output operations needed. Once the file is open we will read the file line by line using the getline() function and count the number of lines.

How do you count lines in a play script?

Most print editions count only spoken lines in numbering lines within a scene, and start afresh with each scene. (MLA uses Hindu-Arabic numerals exclusively, separated by periods, for citations by act and scene or by act, scene, and line.)


Video Answer


1 Answers

Normally you read files in C using fgets. You can also use scanf("%[^\n]"), but quite a few people reading the code are likely to find that confusing and foreign.

Edit: on the other hand, if you really do just want to count lines, a slightly modified version of the scanf approach can work quite nicely:

while (EOF != (scanf("%*[^\n]"), scanf("%*c"))) 
    ++lines;

The advantage of this is that with the '*' in each conversion, scanf reads and matches the input, but does nothing with the result. That means we don't have to waste memory on a large buffer to hold the content of a line that we don't care about (and still take a chance of getting a line that's even larger than that, so our count ends up wrong unless we got to even more work to figure out whether the input we read ended with a newline).

Unfortunately, we do have to break up the scanf into two pieces like this. scanf stops scanning when a conversion fails, and if the input contains a blank line (two consecutive newlines) we expect the first conversion to fail. Even if that fails, however, we want the second conversion to happen, to read the next newline and move on to the next line. Therefore, we attempt the first conversion to "eat" the content of the line, and then do the %c conversion to read the newline (the part we really care about). We continue doing both until the second call to scanf returns EOF (which will normally be at the end of the file, though it can also happen in case of something like a read error).

Edit2: Of course, there is another possibility that's (at least arguably) simpler and easier to understand:

int ch;

while (EOF != (ch=getchar()))
    if (ch=='\n')
        ++lines;

The only part of this that some people find counterintuitive is that ch must be defined as an int, not a char for the code to work correctly.

like image 165
Jerry Coffin Avatar answered Oct 20 '22 19:10

Jerry Coffin