Friday, January 6, 2012

c program to count number of lines in a file


2 comments:

  1. // Author-- Ashutosh Tripathi
    // www.facebook.com/omi.pro

    #include
    #include
    void main()
    {
    FILE *fp; int c, ct_lines;
    ct_lines = 0;
    fp = fopen("foo.txt", "rb");
    do {
    c = fgetc(fp);
    if (c == '\n')
    ct_lines++;
    } while (c != EOF);
    fclose(fp);
    printf("Number of lines are: %d"ct_lines);
    getch();
    }

    ReplyDelete
    Replies
    1. Hi Ashutosh,

      That too will work.
      I have presented a generic example which will take the input from us.

      Thanks,
      Saurabh Gupta
      saurabh.gupta@ccplusplus.com

      Delete