Thread: Simple Line Parsing

  1. #16
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Thanks a lot. Now I'm using :

    Code:
    while(fgets(s,1000,f)!=NULL)
        {
          char *token=s;
          char buffer[1000];
          int n = 0;
          int i = 0;
          printf("line %d:\n",++k);
          for(i=0;*token;i++)
          {
    	size_t len= strcspn(token," \n");
    	n = sprintf(buffer,"%*.*s", (int)len, (int)len, token);
    	printf ("%d\n",buffer);
    	token= token+len+1 ;
          }
    
      }
    I'm getting the o/p as:
    line 1:
    1
    2
    3

    But I want to access each element of buffer as integer(e.g. I want 1,2,3 as integers separately). Can I use atoi for this? Also, how to access each element of buffer?I'm using buffer[0] (expecting to find the string "1") but getting
    0
    0
    0
    instead.

    I'll obliged if you could explain.

  2. #17
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I don't suppose you could post a bit more code for context? I would like to see a few more of your variables...

  3. #18
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by AngKar
    But I want to access each element of buffer as integer(e.g. I want 1,2,3 as integers separately).
    If you are parsing a whitespace-delimited file of numbers, I would instead do something like this.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file )
       {
          int value[20];
          size_t i, size;
          for ( i = 0; i < sizeof value / sizeof *value; ++i )
          {
             if ( fscanf(file, "%d", &value[i]) != 1 )
             {
                break;
             }
          }
          fclose(file);
    
          for ( size = i, i = 0; i < size; ++i )
          {
             printf("value[%lu] = %d\n", (long unsigned)i, value[i]);
          }
       }
       return 0;
    }
    
    /* file.txt
    1 2 3
    1 2 4
    2 3 4
    */
    
    /* my output
    value[0] = 1
    value[1] = 2
    value[2] = 3
    value[3] = 1
    value[4] = 2
    value[5] = 4
    value[6] = 2
    value[7] = 3
    value[8] = 4
    */
    [edit]
    Quote Originally Posted by AngKar
    I'm using buffer[0] (expecting to find the string "1")
    You'd likely want buffer.
    Last edited by Dave_Sinkula; 12-28-2005 at 10:57 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #19
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Thats great. I'll try and run it in my m/c. But could you please explain where you are taking care of " " (space) and "\n" (newline)?

    I mean that the i/p file has the format:
    1(space)2(space)3(newline)
    4(space)5(space)6(newline)
    .
    .
    .
    and so on.
    Also could you please shed some light on the 1st for loop condition?

    And I'll be obliged if anyone could direct me to a good string handling url in C.

    Thanks

  5. #20
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by AngKar
    But could you please explain where you are taking care of " " (space) and "\n" (newline)?
    With the %d. Leading whitespace is ignored; the trailing whitespace is not part of a number so it breaks the conversion, and this whitespace becomes leading whitespace for the next number. Both a space ' ' and a newline '\n' are whitespace, as is a tab '\t' and others like '\r' and '\v'.

    If each line will have a different number of values, say like this:
    Code:
    1 2 3
    1
    2 4
    2 3 4
    then I might suggest something else that may be closer to your previous attempt with reading lines -- but using %d in a sscanf loop: sort of a cross between your last version and my last post.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parsing command line strings
    By John_L in forum C Programming
    Replies: 15
    Last Post: 05-28-2008, 08:26 AM
  2. Imposing Line Numbers automatically in the C code
    By cavestine in forum C Programming
    Replies: 14
    Last Post: 10-15-2007, 12:41 AM
  3. Command Line Argument Parsing
    By lithium in forum Windows Programming
    Replies: 3
    Last Post: 07-13-2005, 07:01 PM
  4. Read only one line using seekg
    By RedZippo in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2004, 11:10 PM
  5. if is faster than switch?
    By skorman00 in forum C++ Programming
    Replies: 32
    Last Post: 03-06-2004, 01:15 PM