how to get line number and column number

This is a discussion on how to get line number and column number within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <ctype.h> typedef QUALITY int; #define PAIRENDLEN 2000 void getSSnumber(char *input,QUALITY *snpCount) { //char buff[BUFSIZ]; ...

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

    how to get line number and column number

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    typedef QUALITY int;
    #define PAIRENDLEN 2000
    void getSSnumber(char *input,QUALITY *snpCount)
    {
         
         //char buff[BUFSIZ];
         FILE *fp;
         char *buf = (char *) calloc (PAIRENDLEN,sizeof(char));
         //char buf[PAIRENDLEN];
         for ( int i = 0; i < PAIRENDLEN; ++i ) buf[i] = '0';
         if ( (fp = fopen(input,"r")) == 0 ){printf("open '%s' error in getSSnumber function\n",input); exit(0);}
         int     isKnownLen = 0;
         QUALITY snpNumber  = 0;
         int i              = 0;
         while( feof(fp) == 0 )
         {     
                   if ( isKnownLen == 0 )
                   {
                        //char *buf = (char *) calloc (1,sizeof(char));
                      
                      
                        char c;
                        i = 0;
                        while( (c = fgetc(fp)) != '\n' )
                        {
                               //*(buf+i) = c;
                               //buf = (char *) realloc (buf,i+2);
                               //printf("%d\n",i+2);
                               ++i;
                               
                        }
                        ++snpNumber;
                        //printf("ss %d\n",i);
                        //if ( snpNumber == 2 ) 
                        isKnownLen = 1;                                   
                   }else
                   {
                  
                        //char *buf;// = "aa";// = (char *) calloc (i,sizeof(char));
    		            //if ( i > PAIRENDLEN ) { buf = (char *) realloc (buf,i); }
    		            int j = i+1;
                        char tmp[j];tmp[0] = '0';
                        char *y = fgets(tmp,j,fp);
                        //printf("%s %d %d %s\n",y,snpNumber,strlen(tmp),tmp);
                        //buf[0] = '0';
                        int x = strlen(tmp);
                        if ( y && x >= 2 ) ++snpNumber; 
                        //++snpNumber;  
                  }                 
                                    
         }
         fclose(fp);
         snpCount[0] = snpNumber;
         snpCount[1] = i;
         //printf("%ld %d\n",snpNumber,i);
         
    }
    
    int main(int argc,char **argv)
    {
    	int e[2];
    	getSSnumber(argv[1],e);
    	printf("total line number:%d total col number:%d\n",e[0],e[1]);
            return 0;
    }
    Is there any bugs in my codes?
    Is there more simple codes to get total line and column number?

    my inputfile is :
    Code:
    AAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAAAAAAA
    .........
    Last edited by zcrself; 12-23-2010 at 10:53 PM.

  2. #2
    Registered User
    Join Date
    Dec 2010
    Posts
    6
    I think it should be typedef int QUALITY

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    After a dynamic allocation of memory, it is common to initialize the area to binary zeros. However, you are initializing the area to character '0'.
    Code:
     
     for ( int i = 0; i < PAIRENDLEN; ++i ) buf[i] = '0';
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
         char *buf = (char *) calloc (PAIRENDLEN,sizeof(char));
         //char buf[PAIRENDLEN];
         for ( int i = 0; i < PAIRENDLEN; ++i ) buf[i] = '0';
    As Dino points out you are incorrectly initilizing your buffer.
    But you are also doing so right after a call to calloc() which correctly initialized it for you.

    The correct way to use calloc is this...
    Code:
    char *buf = calloc(PAIRDLEN + 1,sizeof(char));
    That guarantees a buffer intialized to nulls with 1 extra character that will always be a null.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. Can anyone help?
    By javalurnin in forum C Programming
    Replies: 11
    Last Post: 12-02-2009, 05:02 AM
  3. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  4. Bitwise OR
    By tinkerbell20 in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2005, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21