Thread: How to sort data in .txt file

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    11

    How to sort data in .txt file

    I have a .txt file with floating point values on each line:
    3.14 5.221 0.55 4.77
    6.12 2.13 9.33 1.2
    0.99 4.22 3.56 8.95 3.125
    9.0575 4.897542 120.2324 55.555
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 256
    int main(int argc, char *argv[])
    {
        if(argc !=2)
        {
            printf("Invalid format.\n");
            return -1;
        }
        FILE *fin = fopen(argv[1], "rt");
        if(!fin)
        {
            printf("ERROR opening the file.\n");
            return 1;
        }
        char buf[MAX];
        int i = 0;
        float v[MAX], n;
        while(fgets(buf, MAX, fin))
        {
            buf[strlen(buf)-1] = '\0';
            char *word = strtok(buf, " ");
            while(word != NULL)
            {
                printf("Word : %s <-> %.3f\n", word, atof(word));
                //v[i] = atof(word);
                //i++;
                n = atof(word);
                word = strtok(NULL, " ");
                printf("%f\n", n);
            }
        }
        fclose(fin);
        return 0;
    }
    I wanna print the sets sorted by the number of digits in the decimal part. I'm thinking of using qsort maybe, but I tried placing each value in an array and only garbage values were printed. Why is that?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ipsilante
    I wanna print the sets sorted by the number of digits in the decimal part.
    You might want to elaborate on this. For example, by "set" you presumably mean a set of values on a line. Do you want to sort the values within each set? Do you want to sort the sets themselves with respect to each other?

    Quote Originally Posted by ipsilante
    I'm thinking of using qsort maybe, but I tried placing each value in an array and only garbage values were printed. Why is that?
    You probably can use qsort, but you're sorting an array of strings, not floats. If you try sorting floats, it simply won't work: how many "digits in the decimal part" is there?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2021
    Posts
    11
    Desired output would be:
    6.12 2.13 9.33 1.2 // 7 digits in total in the decimal part
    3.14 5.221 0.55 4.77 // 9 digits in total in the decimal part
    0.99 4.22 3.56 8.95 3.125 // 11 digits in total in the decimal part
    9.0575 4.897542 120.2324 55.555 // 17 digits in total in the decimal part


  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well that was nothing like I expected.
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main ( ) {
      const char *seps = " \n";
      FILE *in = fopen("foo.txt","r");
      char buff[BUFSIZ];
      while ( fgets(buff,BUFSIZ,in) ) {
        char *p;
        for ( p = strtok(buff,seps) ; p ; p = strtok(NULL,seps) ) {
          char *q = strchr(p,'.');
          printf("Len after . in %s is %zd\n", p, strlen(q+1));
        }
      }
      fclose(in);
    }
    
    $ ./a.out 
    Len after . in 3.14 is 2
    Len after . in 5.221 is 3
    Len after . in 0.55 is 2
    Len after . in 4.77 is 2
    Len after . in 6.12 is 2
    Len after . in 2.13 is 2
    Len after . in 9.33 is 2
    Len after . in 1.2 is 1
    Len after . in 0.99 is 2
    Len after . in 4.22 is 2
    Len after . in 3.56 is 2
    Len after . in 8.95 is 2
    Len after . in 3.125 is 3
    Len after . in 9.0575 is 4
    Len after . in 4.897542 is 6
    Len after . in 120.2324 is 4
    Len after . in 55.555 is 3
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-29-2014, 04:02 AM
  2. How to sort the data???
    By justin8077 in forum C++ Programming
    Replies: 5
    Last Post: 08-24-2011, 09:48 AM
  3. Sort a 2d Array and keep data intact
    By rogster001 in forum Contests Board
    Replies: 13
    Last Post: 01-23-2011, 01:09 AM
  4. How To SORT DATA In Database With C/C++?
    By javacvb in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2003, 10:53 AM
  5. sort data
    By sdchem in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2003, 08:40 AM

Tags for this Thread