Thread: Read tab delimited file with selected output

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    4

    Unhappy Read tab delimited file with selected output

    Hi,

    I'm new to C programming. I have a question and I'm stuck for weeks. I have a file (example: abc.txt) with the following values, tab delimited:

    a1 \t b4 \t c5 \t 1
    a5 \t b1 \t c3 \t 1
    a6 \t b2 \t c4 \t 1
    a2 \t b3 \t c7 \t 2
    a1 \t b4 \t c5 \t 2
    a5 \t b1 \t c3 \t 2
    a6 \t b2 \t c4 \t 3
    a2 \t b3 \t c7 \t 3
    a2 \t b3 \t c7 \t 3
    And I want only the selected column 4 with the number 2 data to pull out to a new file lets say def.txt, and the data will look like this:

    a2 \t 2
    a1 \t 2
    a5 \t 2
    How can I achieve doing that? I'm using C, my head is like gonna explode looking at it. Here's my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(void)
    {
    
    
       static const char filename[] = "abc.txt";
       FILE *file = fopen(filename, "r");
    
    
       if ( file != NULL )
       {
          char line[BUFSIZ];
    
    
    	  char col1[BUFSIZ], col2[BUFSIZ], col3[BUFSIZE];
    	  int col4[BUFSIZ];
    
    
          while ( fgets(line, sizeof line, file) != NULL )
          {
             if ( sscanf(line, "%s %s %s %s", col1, col2, col3, &col4) == 4 )
               {
    			   if (col4 == 2)
    			   {
    			     printf("%s\t%s\n", col1, col4);
    			   }
               }
    
    
    	  } 
    	  system("pause");
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    I haven't write the write on new file part, still stuck on sorting out the data part. Need advice and thank you in advance!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    So is there a problem with your code? Does this code compile without errors or warnings? If not post the entire error/warning messages, exactly as they appear in your development environment.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    scanf with a %s will consume both the alpha character and the number. your scanf needs to be more granular. and col4 doesn't need to be an array if it is a single number
    Code:
    int main()
    {
            const char *s = "a1\tb2\tc3\td4";
            char a;
            char b;
            char c;
            char d;
            int  i;
            int  j;
            int  k;
            int  l;
            sscanf(s,"%c%d %c%d %c%d %c%d",&a,&i,&b,&j,&c,&k,&d,&l);
            printf("%c\n",a);
            printf("%d\n",i);
            printf("%c\n",b);
            printf("%d\n",j);
            printf("%c\n",c);
            printf("%d\n",k);
            printf("%c\n",d);
            printf("%d\n",l);
        return 0;
    }
    this only works if you only have a single character then a number. the number can be more than 1 character long.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read and process+output to file
    By jochen in forum C Programming
    Replies: 4
    Last Post: 11-03-2007, 02:22 PM
  2. Read from file, output random line
    By Asbestos in forum C++ Programming
    Replies: 6
    Last Post: 10-29-2005, 02:53 PM
  3. Replies: 3
    Last Post: 09-14-2005, 09:20 PM
  4. I am lost on how to read from file and output to file?
    By vicvic2477 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 11:52 AM
  5. Replies: 1
    Last Post: 12-31-2001, 05:04 PM

Tags for this Thread