Hi all,
I wanted to write a program which will count the number of lines in a text file. And I tried to modify a example to do this. This is what I have written:
It doesn`t work. It counts the number of characters.Code:/* This program counts the lines in a text file */ #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int ch; FILE *fp; long NumberOfLines = 0; if (argc != 2) { printf ("Usage: %s filename\n", argv[0]); exit(1); } if ((fp = fopen(argv[1], "r")) == NULL) { printf("Can`t open %s\n", argv[1]); exit(1); } while ((ch = getc(fp)) != EOF) { if (ch = "\n") NumberOfLines++; } fclose(fp); printf("File %s has %ld lines\n", argv[1], NumberOfLines); return 0; }
My question is how does the program understands when it comes to the end of the line? There`s EOF for end-of-file, is there sth similar for end-of-line?
Thanks in advance for your answers.



LinkBack URL
About LinkBacks



?