Thread: Reading colums with tab spaces

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    Reading colums with tab spaces

    Hello All,
    I have to read a file which contains colums such as below.
    Code:
    Net	Name		#Pins	Driver		Recvs. (block, pin)
    
    0	i_63_		3	(   0,  -1)	( 180,   0)	( 226,   0)
    1	i_50_		14	(   1,  -1)	( 218,   0)	( 245,   0)	( 253,   0)	( 266,   0)	( 287,   0)	( 291,   0)	( 302,   1)
    I need to read the Net, then the Driver column and then the Recvs colum i.e. I need 0 then (0,-1) then (180,0) and then (226,0). I need these variables to compare with another file so I would typically require them to be stored as ints and after each line I compare them wioth another file then read the next line and so on. The number of colums is not fixed and the columns are separated by tabs.
    I am a little confused as to how to proceed. Any help?
    Thanks all.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    why not use fread() function to read a whole record at a time into the sturcture and then u can get what ever field u wanted like net and driver.

    s.s.harish

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    I cannot understand how to read the fields. I do use fgets to read line by line from the file. But I cannot understand how to read specifically the "0" then the "180" and so on.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You may want to use fscanf something like this.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       static const char filename[] = "test.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          int  i, net, pins;
          char name[14];
    
          fscanf(file, "%*[^\n]%*c%*[^\n]%*c"); /* read and discard first 2 lines */
    
          while ( fscanf(file, "%d\t%13[^\t]\t\t%d", &net, name, &pins) == 3 )
          {
             printf("net = %d, name = \"%s\", pins = %d:\n", net, name, pins);
             for ( i = 0; i < pins; ++i )
             {
                int block, pin;
                if ( fscanf(file, "\t(%d,%d)", &block, &pin) == 2 )
                {
                   printf(" %2d - block = %d, pin = %d\n", i, block, pin);
                }
             }
          }
          fclose(file);
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    
    /* my output
    net = 0, name = "i_63_", pins = 3:
      0 - block = 0, pin = -1
      1 - block = 180, pin = 0
      2 - block = 226, pin = 0
    net = 1, name = "i_50_", pins = 14:
      0 - block = 1, pin = -1
      1 - block = 218, pin = 0
      2 - block = 245, pin = 0
      3 - block = 253, pin = 0
      4 - block = 266, pin = 0
      5 - block = 287, pin = 0
      6 - block = 291, pin = 0
      7 - block = 302, pin = 1
    */
    But you could also continue to use sscanf if you messed around with a %n to keep track of where you end up in the line of text.
    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
    Mar 2005
    Posts
    18
    Thanks,
    I was using sscanf but ran into few problems as I could not keep track on the elements, so I used fscanf.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  2. reading parts of a tab delimited file
    By AngKar in forum C Programming
    Replies: 8
    Last Post: 01-24-2006, 10:38 PM
  3. Replies: 4
    Last Post: 10-14-2005, 12:53 PM
  4. Tab or No Tab :: C++ Compilers
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 02-08-2002, 07:39 PM
  5. Spaces into tab characters
    By IceCold in forum C Programming
    Replies: 1
    Last Post: 09-09-2001, 11:31 AM