Thread: Determine how many lines are in a file

  1. #1
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214

    Determine how many lines are in a file

    hi , is there a way to determine how many lines are in a file?

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    19
    open the file in textmode
    initialialise a counter.
    get ch using fgetc
    increment counter on each '\n


    int lines=0;
    char ch;
    fp=fopen("file.txt","r");
    while((ch=fgetc(fp))!=EOF)
    {
    if (ch=='\n') { lines++; }
    }
    fclose(fp);

    printf("lines are %d",lines);

  3. #3
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    thanks

  4. #4
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >open the file in textmode
    >initialialise a counter.
    >get ch using fgetc
    >increment counter on each '\n
    What if the file doesn't end with a newline? This would work a little better for that case:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      FILE *fp;
      int   lines = 0;
      char  buffer[BUFSIZ];
    
      fp = fopen("file.txt", "r");
      while (fgets(buffer, sizeof buffer, fp)) {
        ++lines;
      }
      fclose(fp);
      printf("lines are %d\n", lines);
    
      return 0;
    }
    But then you have the problem of lines that are longer than the buffer. So one might fall back to the reading of a single character with a few additions to handle end-of-file:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      FILE *fp;
      int   lines = 0;
      char  ch;
    
      fp = fopen("file.txt", "r");
      for (;;) {
        ch = getc(fp);
        if (ch == '\n') {
          ++lines;
        }
        if (ch == EOF) { /* Assume the end */
          ++lines;
          break;
        }
      }
      fclose(fp);
      printf("lines are %d\n", lines);
    
      return 0;
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by sunil21
    open the file in textmode
    initialialise a counter.
    get ch using fgetc
    increment counter on each '\n


    int lines=0;
    char ch;
    fp=fopen("file.txt","r");
    while((ch=fgetc(fp))!=EOF)
    {
    if (ch=='\n') { lines++; }
    }
    fclose(fp);

    printf("lines are %d",lines);
    Time for me to play forum police again.

    1) Don't post code at all, ever, if you aren't going to use [code] tags!
    2) You would have known this, had you actually read the forum Announcements like you were supposed to.

    Go read all of the forum Announcements, and abide by them, or don't bother posting.

    Now on to some other problems...
    Quote Originally Posted by Robc
    Code:
      char  ch;
    
      fp = fopen("file.txt", "r");
      for (;;) {
        ch = getc(fp);
        if (ch == '\n') {
          ++lines;
        }
        if (ch == EOF) { /* Assume the end */
          ++lines;
          break;
        }
    Welcome to one of the most common bugs on the forum. EOF can never be stored in a char. Use an int.
    (That is, after all, why getc returns an int and not a char. )

    [edit]Didn't like the way the lines were wrapping...[/edit]

    Quzah.
    Last edited by quzah; 07-14-2004 at 02:44 PM.
    Hope is the first step on the road to disappointment.

  6. #6
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >Welcome to one of the most common bugs on the forum. EOF can never be stored in a char. Use an int.
    ACK! Okay, I'm never showing my face here again. I can't believe I did that. This account makes me stupid, I think I'll go back to the old one even though it's broken.

  7. #7
    .
    Join Date
    Nov 2003
    Posts
    307
    This is code from GNU wc for linecounting:
    Code:
          while ((bytes_read = safe_read (fd, buf, BUFFER_SIZE)) > 0)
    	{
    	  register char *p = buf;
    
    	  if (bytes_read == SAFE_READ_ERROR)
    	    {
    	      error (0, errno, "%s", file);
    	      exit_status = 1;
    	      break;
    	    }
    
    	  while ((p = memchr (p, '\n', (buf + bytes_read) - p)))
    	    {
    	      ++p;
    	      ++lines;
    	    }
    	  bytes += bytes_read;
    	}
        }
    safe_read is a call to read() that accounts for the fact that read returns (-1) when there are conditions that are not usually considered errors - e.g., EINTR.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>fp = fopen("file.txt", "r");
    ... and don't forget to error check the value of fp before using it.

    To the OP: Out of interest, why do you need to know the number of lines anyway? Is this just an exercise, or do you have real requirement to know this value?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >... and don't forget to error check the value of fp before using it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 08:59 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Reading Lines From a File
    By Dangerous Dave in forum C++ Programming
    Replies: 6
    Last Post: 02-22-2005, 01:17 PM