Thread: why not to get line number of one file?

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    168

    why not to get line number of one file?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main( int argc, char **argv)
    {
        char *input = argv[0];
        FILE *fp;
        if ( (fp = fopen(input,"r")) == 0 ){printf("open '%s' error.\n",input); exit(0);}
        int i = 0;
        while( feof(fp) == 0 )
        {
               char buf[100];
               fgets(buf,100,fp);
               if ( strlen(buf) >= 2 ) ++i;
               printf("%s\n",buf);
        }
        printf("line number:%d\n",i);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You should give us more info.
    Question 12.2
    Don't use feof() to control loop condition.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Bayint Naung View Post
    You should give us more info.
    Question 12.2
    Don't use feof() to control loop condition.
    Code:
        while( fgets(buf,100,fp) != NULL )
        {
               
               //char *x = fgets(buf,100,fp);
               i++;
               printf("%s\n",buf);
               //fputs(buf, fp);
        }
        printf("line number:%d\n",i);
    get wrong line number, why?
    Last edited by zcrself; 10-14-2010 at 12:50 AM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Each line ends with newline.
    So count the newlines.
    Extrapolate line numbers from this information.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    What do you mean by 'can't get line number'?!
    Is it giving you wrong line number or what???

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Bayint Naung View Post
    What do you mean by 'can't get line number'?!
    Is it giving you wrong line number or what???
    Yes.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Can attach your text file? and post output of your program.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Bayint Naung View Post
    Can attach your text file? and post output of your program.
    program:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int getLineCount(char *input)
    {
        FILE *fp;int i = 0; char buf[100];
        memset(buf,0x00,sizeof(buf));
        if ( (fp = fopen(input,"r")) == 0 ){printf("open '%s' error.\n",input); exit(0);}
        while( fgets(buf,44,fp) != NULL )
        {
               i++;
               //printf("%d %s\n",i,buf);
        }
        return i;
    }
    
    
    int main( int argc, char **argv)
    {
        char *input = argv[0];
        
        
        printf("line number:%d\n",getLineCount(input));
        return 0;
    
    }
    input:
    Code:
    @HWI-EAS-249_38:2:1:2:857.R
    AAAGTATGTAGGACTCATGNNNNTNNNNNNNCNNNNNNAN
    +
    ?8BBBBBBBBBB;BB<'<,'''';'''''''9'''''','
    output:
    Code:
    line number:180
    Last edited by zcrself; 10-14-2010 at 02:08 AM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > get wrong line number, why?
    Unless you know FOR SURE that every line is <98 characters in length, then you're going to end up counting the number of lines AND the number of 99-character blocks that make up long lines.

    Make sure buf contains a \n before incrementing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Salem View Post
    > get wrong line number, why?
    Unless you know FOR SURE that every line is <98 characters in length, then you're going to end up counting the number of lines AND the number of 99-character blocks that make up long lines.

    Make sure buf contains a \n before incrementing.
    I can't understand your answer clearly

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I would declare char buff[] at the top of the function, and NOT inside the while loop.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Salem is saying that, if your input file has any line longer than the buffer, the line count will be incorrect. That's because of the way fgets() works when it encounters long lines.

    Read the documentation for more information on how fgets() works.

    Furthermore, if you have an input file with non-text characters, you may get different results too (although counting lines in such a file is rather an academic exercise).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-04-2010, 07:24 AM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. Very strange error...
    By Blackroot in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2006, 01:37 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM