Count number of line using C

后端 未结 5 1392
无人共我
无人共我 2020-12-16 07:58

Is there a way to count the number of lines in my file using C?

5条回答
  •  长情又很酷
    2020-12-16 08:27

    If you want to perform this programmatically, open the file in text mode and perform fgetc() operation until you reach end of file. Keep a count of number of times fgetc was called.

        FILE *fp = fopen("myfile.txt");
        int ch;
        int count=0;
        do
        {
            ch = fgetc(fp);
            if(ch == '\n') count++;   
        } while( ch != EOF );    
    
        printf("Total number of lines %d\n",count);
    

提交回复
热议问题