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.
/* This program counts the lines in a text file */
int main(int argc, char *argv)
long NumberOfLines = 0;
if (argc != 2)
printf ("Usage: %s filename\n", argv);
if ((fp = fopen(argv, "r")) == NULL)
printf("Can`t open %s\n", argv);
while ((ch = getc(fp)) != EOF)
if (ch = "\n") NumberOfLines++;
printf("File %s has %ld lines\n", argv, NumberOfLines);
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.