A
Alex
CBFalconer said:Charles M. Reinke said:From: "ThunderBird said:it mentioned "You could count the numbers of linebreaks. ".
My question is how to count the numbers of linebreaks?
This works for me...
cmreinke@hologram>cat lines.c
/* lines.c */
#include <stdio.h>
int main(int argc, char **argv) {
long lines=0;
FILE *fp;
if(argc>1) {
if((fp=fopen(argv[1], "r"))) {
while(fscanf(fp, "%*[^\n]\n")!=EOF) lines++;
printf("Number of lines in file \"%s\": %ld\n", argv[1], lines);
} /* if */
else printf("ERROR: could not open file \"%s\"\n", argv[1]);
} /* if */
else printf("ERROR: no input file specified\n");
return 0;
} /* main */
/* lines.c */
Why so complex? Use stdin.
[1] c:\c\wc>cat lc.c
#include <stdio.h>
int main(void)
{
unsigned long chars, lines;
int ch;
chars = lines = 0;
while (EOF != (ch = getchar())) {
chars++;
if ('\n' == ch) lines++;
}
printf("%lu chars in %lu lines\n", chars, lines);
return 0;
} /* main lines */
[1] c:\c\wc>cc lc.c -o lc.exe
[1] c:\c\wc>.\lc < lc.c
285 chars in 15 lines
WOW! Although not the original poster, I tried that. It works sweet. Thnx.