reading parts of a tab delimited file

This is a discussion on reading parts of a tab delimited file within the C Programming forums, part of the General Programming Boards category; Hi, Could anyone please suggest me a way to read parts from a tab -delimted file i.e. I want to ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    reading parts of a tab delimited file

    Hi,

    Could anyone please suggest me a way to read parts from a tab -delimted file i.e. I want to read data of the 3rd and the 5th columns?

    if my data is:

    1234 \t 2342 \t qwert \t iuy1 \t hgy7
    2323 \t 2323 \t fghjj \t sdsds \t dwe3
    .
    .
    .

    I want to read :
    str A =" qwert"
    and
    str B = "hg7y"


    Thanks,
    AK

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The usual suspects: fgets + sscanf. A skeleton might be as follows.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          char line[BUFSIZ];
          while ( fgets(line, sizeof line, file) != NULL )
          {
             char s[20];
             if ( sscanf(line, "%*s %*s %19s", s) == 1 )
             {
                puts(s);
             }
          }
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    
    /* my output
    qwert
    fghjj
    */
    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.*

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    I'll be obliged if you can explain the sscanf line. how is it handling the two columns? what if there were 3 columns, say 1,3 and 5?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    http://web.archive.org/web/200502070...t.html#4.9.6.2

    [edit]
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          char line[BUFSIZ];
          while ( fgets(line, sizeof line, file) != NULL )
          {
             char one[20], three[22], five[18];
             if ( sscanf(line, "%19s %*s %21s %*s %17s", one, three, five) == 3 )
             {
                printf("one = \"%s\", three = \"%s\", five = \"%s\"\n",
                       one, three, five);
             }
          }
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    
    /* my output
    one = "1234", three = "qwert", five = "hgy7"
    one = "2323", three = "fghjj", five = "dwe3"
    */
    Last edited by Dave_Sinkula; 01-23-2006 at 11:29 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.*

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    got u... u rock...

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Also, if I've a file which have first 4 lines which I don't want to read, how can I move the pointer to the 5th line to start reading?
    Is using 4 fgets a good idea?

    AK

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Perhaps use fgets in a loop to read (and discard) the number of lines you enter a loop with those that you do care about.
    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.*

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    But I think there is some problem with it. I use:

    Code:
    
    char c[120];
    
    for (i=0;i<4;i++){
    
    fgets (c,120,fp);
    puts(c);
    
    }
    and it puts only the 1st line. Is there any way of flushing c?

  9. #9
    old man
    Join Date
    Dec 2005
    Posts
    90
    (edited) Try fflush(NULL) ... it should flush all open streams.
    Last edited by eerok; 01-24-2006 at 10:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Reading from file into structs..
    By dankas in forum C Programming
    Replies: 14
    Last Post: 10-16-2002, 11:33 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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