Thread: Data sorting with tab delimited .txt input file

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

    Arrow Data sorting with tab delimited .txt input file

    0 0 15.5 15.5 -29.671 81.879 0.149821 -0.207526 0.000906136 -0.00125514 0.00154805 0
    1 0 39.5 15.5 -28.7275 81.879 0.241249 -0.199974 0.0014591 -0.00120947 0.0018952 0
    2 0 63.5 15.5 -27.784 83.879 0.113511 -0.208251 0.00068653 -0.00125952 0.00143448 0
    3 0 87.5 15.5 -26.8405 83.879 0.262949 -0.119694 0.00159035 -0.000723924 0.00174736 0
    4 0 111.5 15.5 -25.897 86.879 0.340391 -0.153879 0.00205873 -0.000930676 0.00225932 0
    5 0 135.5 15.5 -24.9535 86.879 0.350416 -0.0832607 0.00211936 -0.00050357 0.00217836 0
    6 0 159.5 15.5 -24.01 88.879 0.237944 -0.0505043 0.00143911 -0.000305456 0.00147117 0
    7 0 183.5 15.5 -23.0665 88.879 0.416603 -0.074264 0.00251966 -0.000449157 0.00255938 0
    8 0 207.5 15.5 -22.1229 89.879 0.442096 -0.140521 0.00267384 -0.00084989 0.00280567 0
    9 0 231.5 15.5 -21.1794 89.879 0.599385 -0.0447657 0.00362515 -0.000270748 0.00363524 0



    Above is my test.txt file, tab delimited. I would want my program to read that file, and pull a few columns to new file lets say output.txt (I haven't code that in my program yet, can ignore that first). I want to pull data from column 5, 6, and 10, and rearrange it in output.txt as column 6, 10, 5 (which is working, ignore the file output part). The problem is, I just want to pick the data from column 5 which the value equals to lets say 86.879, and the output data should look like this:


    86.879 -0.000930676 -25.897
    86.879 -0.00050357 -24.9535


    How can I get it done? Below is my coding. Thanks in advance!


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    
    
       static const char filename[] = "test.txt";
       FILE *file = fopen(filename, "r");
    
    
       if ( file != NULL )
       {
          signed char *line[BUFSIZ];
    
    
       signed char col5[BUFSIZ], col6[BUFSIZ], col10[BUFSIZ], col6x[BUFSIZ];
    
    
     // remove the slashes             printf("Data fetch coordinate x[mm]: ");
     // remove the slashes           scanf("%d", &col6x);
    
    
          while ( fgets(line, sizeof line, file) != NULL )
          {
             if ( sscanf(line, "%*s %*s %*s %*s %s %5s %*s %*s %*s %s", &col5, &col6, &col10) == 3 )
               {
          if ( col6x == col6 )
          {
            printf("%s\t%s\t%s\n", col6, col10, col5);
          }
               }
    
    
       } 
       system("pause");
       }
       else
       {
          perror(filename);
       }
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2012
    Posts
    4
    Complied and tried to enter 86.879 or 86.87 (with the slashes removed in the code), it returned nothing.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > signed char *line[BUFSIZ];
    You want an array of char, not an array of char pointers.
    So
    signed char line[BUFSIZ];

    > if ( sscanf(line, "%*s %*s %*s %*s %s %5s %*s %*s %*s %s", &col5, &col6, &col10) == 3 )
    1. Drop the & from all the variables. An array name is already a pointer to a char, so nothing is gained by using &.
    If the variables were really pointers to begin with, then using & would be very wrong indeed.
    2. Why %5s when the string itself is already 6 chars long? All it does is make it harder - the buffer allocated is big enough.

    > if ( col6x == col6 )
    Lookup strcmp()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    4
    Thanks Salem, I get what you mean. Once I'm done will post the working code. Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comma delimited text sorting, advice?
    By rollyahh in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2011, 05:17 AM
  2. Adding data to existing data in an input file?
    By matthayzon89 in forum C Programming
    Replies: 4
    Last Post: 11-20-2010, 11:23 PM
  3. sorting data from file input
    By bigzeppelin2k in forum C Programming
    Replies: 2
    Last Post: 11-18-2003, 02:23 PM
  4. Sorting data retrieved from input file
    By Minds_I in forum C Programming
    Replies: 3
    Last Post: 07-20-2003, 06:25 PM
  5. reading delimited input from file
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 07-20-2002, 04:18 PM