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

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.

Leave a Comment