\C
linecounter.c
/* line count example */ #includeint main(int argc, char *argv[]) { long int count = 0, c = 0; FILE *fp; /* check arguments */ if (argc != 2) { /* display usage information */ fprintf(stderr, "usage.. %s \n", argv[0]); exit(-1); /* exit on error */ } fp = fopen(argv[1], "r"); /* open the file */ /* if we can't open the file, display an error */ if (fp == NULL) { fprintf(stderr, "%s: can't open %s.\n", argv[0], argv[1]); exit(-1); /* exit on error */ } /* while the next character isn't EOF, check for the next line character */ while ((c = fgetc(fp)) != EOF) { /* if the character recieved from the file is '\n' then increase the line count by one */ if (c == '\n') count++; } fclose(fp); /* close the file */ /* display results */ printf("%s: %s has %d lines.\n", argv[0], argv[1], count); return(0); /* return OK */ }