Looking at the sample implementation of wc.c when counting number of lines, it loop through the file, one character at a time and accumulating the '\n' to count the number of newlines:
#define COUNT(c) \
ccount++; \
if ((c) == '\n') \
lcount++;
Is there a way to just seek the file for '\n' and keep jumping to the newline characters and do a count?
Would seeking for '\n' be the same as just reading characters one at a time until we see '\n' and count it?
Well, all characters are not
'\n', except for one. A branch-less algorithm is likely to be faster.Have you tried
std::count, though?Compiler Explorer
Or with a file:
Compiler Explorer