PDA

View Full Version : get number of lines in a text file c/c++



monkeyking
February 2nd, 2009, 03:14 AM
quite a few times I have to count the number of lines in a textfile.
And I always end up with something like



while(!.eof()){
.getline(...);
numLines1++;
}
return numLines;


This of cause works. But It annoys me that I have to read in all lines of a file. Does anyknow know of a simpler or more beautifull way.

thanks in advance.

Nemooo
February 2nd, 2009, 06:57 PM
I don't know if this is faster. Basically it's just counting the number of characters until it reaches EOF.


int line_num(char *filename)
{
FILE *f;
char c;
int lines = 0;

f = fopen(filename, "r");

if(f == NULL)
return 0;

while((c = fgetc(f)) != EOF)
if(c == '\n')
lines++;

fclose(f);

if(c != '\n')
lines++;

return lines;
}

Krupski
February 2nd, 2009, 09:38 PM
quite a few times I have to count the number of lines in a textfile.
And I always end up with something like



while(!.eof()){
.getline(...);
numLines1++;
}
return numLines;


This of cause works. But It annoys me that I have to read in all lines of a file. Does anyknow know of a simpler or more beautifull way.

thanks in advance.

Honestly, I don't think there IS any way to tell how many lines a file has other than by counting them all.

Be careful though... DOS/Windows files with CR/LF endings, UNIX files with LF-only endings and MAC files with CR-only endings can confuse functions like getline() or fgets(), so be sure you make accommodations for those variations.

-- Roger