Thread: Sort my file

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    159

    Sort my file

    Hi

    Can someone tell me how can i sort my file created?

    The file writed comes from a struct

    The fields are char name , float total, int result

    My file is printed like this

    Name %% result


    I want to sort from result the highest to the lower


    Any help??

    Thanks

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If the data will fit in RAM, you could use qsort. Remember to include stdlib.h.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Thanks

    But i need to read the file into an array, right?

    file xx
    int array[200]

    array=(fread.....


    is like this?

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    FFS you ask so many mundane questions seemingly without trying to solve the problem/researching/reading other answers first. Just look what a basic google search about quicksort will tell you.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Gil Carvalho View Post
    But i need to read the file into an array, right?

    file xx
    int array[200]
    array=(fread.....

    is like this?
    is not like that

    How could you read the records of the file into an int array?
    You would read it into an array of structs.
    Then write a comparison function for qsort that compares the "result" member.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Hi

    Already did some progress but the data comes unsorted

    Code:
    int comparador(const void * elem1, const void * elem2)
    {
        return ((struct jogoJogado *)elem1)->res_final = ((struct jogoJogado *)elem2)->res_final;
    }
    
    
    int comp()
    {
        struct jogoJogado *listas[10];
        FILE *f;
        f=fopen("jogos.dat", "rb");
        if(!f)
        {
            printf("Erro");
        }
        else
        {
          listas[0] = (struct jogoJogado*)malloc(sizeof(struct jogoJogado));
          int i;
          int retorno=fread(listas[0], sizeof(struct jogoJogado), 1, f);
          for(i=1;retorno==1;i++)
          {
              listas[i]=(struct jogoJogado*)malloc(sizeof(struct jogoJogado));
              retorno= fread(listas[i], sizeof(struct jogoJogado), 1, f);
    
    
            }
        }
        qsort(listas, 4, sizeof(struct jogoJogado), comparador);
        int i;
        for(i=0; i<4; i++)
        {
            printf("%s   %d    %.2f   \n", listas[i]->nome, listas[i]->res_final, listas[i]->p_certas );
        }
        Sleep(4000);
    return 0;
    }
    Whats wrong??

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Wrong info in post; see next post.

    Tim S.
    Last edited by stahta01; 06-21-2012 at 11:43 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    man qsort:
    [...] The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. If two members compare as equal, their order in the sorted array is undefined. [...]
    What does your comparison function return?

    Bye, Andreas

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Code:
    return ((struct jogoJogado *)elem1)->res_final = ((struct jogoJogado *)elem2)->res_final;
    This doesn't do what you think it does.

    Code:
          listas[0] = (struct jogoJogado*)malloc(sizeof(struct jogoJogado));
          int i;
          int retorno=fread(listas[0], sizeof(struct jogoJogado), 1, f);
          for(i=1;retorno==1;i++)
          {
              listas[i]=(struct jogoJogado*)malloc(sizeof(struct jogoJogado));
              retorno= fread(listas[i], sizeof(struct jogoJogado), 1, f);
     
     
            }
    This bit of code will always allocate one more structure than what you read in, and "i" will be one too high. It can also be shortened and simplified (which usually translates to clarity and ease of understanding what it does).

    Code:
        qsort(listas, 4, sizeof(struct jogoJogado), comparador);
    Where did 4 come from?

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Quote Originally Posted by christop View Post
    Code:
    return ((struct jogoJogado *)elem1)->res_final = ((struct jogoJogado *)elem2)->res_final;
    This doesn't do what you think it does.

    That was wrong, i didn't see it was - and not =


    Code:
          listas[0] = (struct jogoJogado*)malloc(sizeof(struct jogoJogado));
          int i;
          int retorno=fread(listas[0], sizeof(struct jogoJogado), 1, f);
          for(i=1;retorno==1;i++)
          {
              listas[i]=(struct jogoJogado*)malloc(sizeof(struct jogoJogado));
              retorno= fread(listas[i], sizeof(struct jogoJogado), 1, f);
     
     
            }
    This bit of code will always allocate one more structure than what you read in, and "i" will be one too high. It can also be shortened and simplified (which usually translates to clarity and ease of understanding what it does).

    I believe it can be more simple than that, someone, check some posts above to (google)

    Code:
        qsort(listas, 4, sizeof(struct jogoJogado), comparador);
    Where did 4 come from?
    4 are the numbers i want to sort.

    To Andreas:

    the print is the following:

    4 Top de jogadores
    *================================================= ============================*

    Before qsort
    Gil 10 80.00
    Baby 9 87.00
    Kill 3 14.00
    cerdo 6 87.00


    after qsort
    Baby 9 87.00
    Gil 10 80.00
    cerdo 6 87.00
    Kill 3 14.00






  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    My question was not about the outcome of the sorting.
    I asked you what does your comparison function return (or better what do you think it returns)?
    And how does this fulfill the requirements qsort() demands?

    Bye, Andreas

  12. #12
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Quote Originally Posted by AndiPersti View Post
    My question was not about the outcome of the sorting.
    I asked you what does your comparison function return (or better what do you think it returns)?
    And how does this fulfill the requirements qsort() demands?

    Bye, Andreas

    The sorting algorithm used by this function compares pairs of values by calling the specified comparator function with two pointers to elements of the array.

    The function does not return any value, but modifies the content of the array pointed by base reordering its elements to the newly sorted order.


    qsort - C++ Reference

    Iv'e been reading, i don't ask anything before google it...

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Then you should continue reading page:
    Parameters
    [...]
    comparator:
    Function that compares two elements. The function shall follow this prototype: int comparator ( const void * elem1, const void * elem2 );

    The function must accept two parameters that are pointers to elements, type-casted as void*. These parameters should be cast back to some data type and be compared.

    The return value of this function should represent whether elem1 is considered less than, equal to, or greater than elem2 by returning, respectively, a negative value, zero or a positive value.
    That's the part explaining why your compare function doesn't work.

    And if you still read further you even get an example of how it works :-)

    Bye, Andreas
    Last edited by AndiPersti; 06-21-2012 at 12:51 PM. Reason: emphasis

  14. #14
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Quote Originally Posted by AndiPersti View Post
    Then you should continue reading page:

    That's the part explaining why your compare function doesn't work.

    And if you still read further you even get an example of how it works :-)

    Bye, Andreas

    Hi Andreas, now it works.

    With the qsort.


    Many thanks

    regards

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O with sort
    By yigster in forum C Programming
    Replies: 2
    Last Post: 06-08-2010, 08:29 PM
  2. Sort a txt File
    By Achilles in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 06:39 AM
  3. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM
  4. How do i sort that file after merge
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 06-14-2002, 08:09 AM
  5. VC++ Sort File
    By Duncan Booth in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2002, 12:06 PM