Thread: Using qsort to read from a file

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    3

    Using qsort to read from a file

    Hello all,

    Trying to understand how to use qsort with arrays and functions reading from a file (bin). I think I have the general idea of how qsort works, but when it comes to reading from a .bin file I am completely lost! My intended output is to make it so so that it comes out in order of destination airport code, with the date and time formatted into a human-readable string. I do not understand how to convert the timestamp using time.h, but I am not worried about that for now.



    Code:
            #include <stdio.h>
            #include <stdlib.h>
            #include <string.h>
    
    
    
    
                    typedef struct MyStruct_struct{
                    char FlightNum[7];
                    char OriginAirportCode[5]; 
                    char DestAirportCode[5];
                    unsigned timeStamp;
                } MyStruct;
                
                
                MyStruct Order[5000];
                
                int compare (const void *v1, const void *v2) 
                {
               
                const MyStruct *ia = (MyStruct *)v1;
                const MyStruct *ib = (MyStruct *)v2;
                    int result=0;
                    if (ia->OriginAirportCode < ia->OriginAirportCode)
                        return -1;
                    else if (ia->OriginAirportCode > ia->OriginAirportCode)
                        return +1;
                    else
                        return 0;
                    
        
                 }
                
                int main(){
                int i;
                  
                    
                         
                     FILE * bin;
                     MyStruct myStruct;
                     
                     bin = fopen("acars.bin", "rb");
                     qsort(Order, 5000, sizeof( MyStruct), compare);
                     for (i = 0; i < 300; i++){
    
    
                     printf("%i) %s, %s, %s\n", i, Order[i].FlightNum, Order[i].OriginAirportCode, Order[i].DestAirportCode);
                     }
                    
                
                   
                    
                    
                    fclose(bin);
                    
                    return 0;
                }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    qsort does not read from files in any way. qsort is a general sort algorithm.

    Try using fread in a loop to fill the Order array.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    3
    Quote Originally Posted by whiteflags View Post
    qsort does not read from files in any way. qsort is a general sort algorithm.

    Try using fread in a loop to fill the Order array.
    Thanks, I don't know how I forgot about that. However, It still is not working.

    If I do
    Code:
                  
                     while(!feof(bin)){ 
                     fread(&myStruct,sizeof(MyStruct),1,bin);
                     qsort(Order, 5000, sizeof( MyStruct), compare);
                     
                       for (i = 0; i < 300; i++)
                     printf("%i) %s, %s, %s\n", i, Order[i].FlightNum, Order[i].OriginAirportCode, Order[i].DestAirportCode);
                     }
    It just loops forever.

    Code:
                while(!feof(bin))
                     { 
                     fread(&myStruct,sizeof(MyStruct),1,bin);
                     qsort(Order, 5000, sizeof( MyStruct), compare);
                     
                      
                     }
                   
                
                    for (i = 0; i < 300; i++) {
                    printf("%i) %s, %s, %s\n", i, Order[i].FlightNum, Order[i].OriginAirportCode, Order[i].DestAirportCode);
                    
                    }
                    fclose(bin);
    Does not work either. It must be something obvious that I am missing. I feel stupid.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by GreenShaw View Post
    Thanks, I don't know how I forgot about that. However, It still is not working.
    Well I wouldn't write the file reading loop the way you are now. It should be something more like:
    Code:
     
        do
        {
            result = fread(&flight, sizeof(flight), 1, fin);
            if (result > 0) {
                Orders[k] = flight;
                ++k;
            }
        } while(result == 1);
    After you read the file like this, it should be okay to sort. Be sure to sort only the amount of flights you read though, or you will have a lot of empty space in Orders affecting the order of the sort.

    If you want to see the snippet working in a larger example, click here.

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    3
    Quote Originally Posted by whiteflags View Post
    Well I wouldn't write the file reading loop the way you are now. It should be something more like:
    Code:
     
        do
        {
            result = fread(&flight, sizeof(flight), 1, fin);
            if (result > 0) {
                Orders[k] = flight;
                ++k;
            }
        } while(result == 1);
    After you read the file like this, it should be okay to sort. Be sure to sort only the amount of flights you read though, or you will have a lot of empty space in Orders affecting the order of the sort.

    If you want to see the snippet working in a larger example, click here.
    I really thank you for all your help, and being patient.But I believe I explained what I was trying to do very poorly. I already have a .bin file, I am not writing to one. I am simply trying to read from an already existing one.

    It would not let me attach the .bin file. So I made a link http://www.filedropper.com/acars.

    Once again, thank you for your patience and understanding. I know now how to atleast write to a file and have a better understanding of void functions, which is something I do have trouble with.

    As for the loop, I have tryed
    do { fread(&myStruct,sizeof(MyStruct),1,bin);

    } while (feof(bin)!=0);





    qsort(Order, 5000, sizeof( MyStruct), compare);
    for (i = 0; i < 200; i++) {
    printf("%i) %s, %s, %s", i, Order[i].FlightNum, Order[i].OriginAirportCode, Order[i].DestAirportCode,Order[i].timeStamp);
    But still no luck.
    Last edited by GreenShaw; 11-28-2015 at 10:30 PM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think you misunderstood why I included an example: I am not interested in your particular bin file. I was merely demonstrating that the loop I showed you should work for a file made up of a bunch of structures like the one you've showed up til now. If the format of the binary file is different, then the approach to reading the file will need to be different.

    You will have to be more specific about what you've tried and what's wrong if you expect more help.

    EDIT: Well, okay, an edited post. Unfortunately, there is not much in common with what you've tried to the loop that I actually posted.

    • You continue to use feof() to control the loop, even though the function is absent in my example.
    • There is no line that actually stores the myStruct you read into the larger Orders array,
    • but my example DOES have a line like that!



    It's no wonder that your results are bad when you are missing these things in the example.
    Last edited by whiteflags; 11-28-2015 at 10:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-07-2014, 07:01 AM
  2. Replies: 7
    Last Post: 12-07-2012, 10:44 PM
  3. Open a file, read it ... and ... read it again
    By Tiago in forum C Programming
    Replies: 1
    Last Post: 04-17-2010, 03:32 AM
  4. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM