Thread: help with c project using qsort

  1. #1
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167

    Unhappy help with c project using qsort

    How do you use qsort on a text file of different length records(when you have to sort two things(record type(first character) and customer_code(chars 2-6))) when the data is in structures inside a union?
    e.g.:
    Code:
      struct i_r_record{
                                char rec_type,
                                cust_code[5],
                                 part_num[6],
                                  i_r_qty[4];
                                };
      struct c_record{
                               char rec_type,
                               cust_code[5],
                               cust_name[20],
                               cust_address[60],
                               cust_bal[9],
                               cust_limit[7];
                             };
      struct d_record{
                                char rec_type,
                                   cust_code[5];
                            };
      union records {
                              struct i_r_records i;
                              struct c_records c;
                              struct d_records d;
                             };
    [code][/code]tagged by Salem

  2. #2
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167

    Post

    I have tried to sort simple arrays, and even an array of structures, but these were all with fields that were of the same length!! I understand the compare function, but not how to implement it in the case of the file of different length records!!

  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
    But you've already read the text file (with its variable length records) into an array like this right?

    union records database[100];

    The union is as long as the longest member, so in memory at least, they're all the same length (which is good enough for qsort)

    In addition, you'll note that the first two members of the union
        char rec_type, cust_code[5],
    are common to all members of the union (which makes things a bit simpler)


    So all you need do is
    Code:
    int compare ( const void *a, const void *b ) {
        const union records *pa = a;
        const union records *pb = b;
        if ( pa->rec_type < pb->rec_type ) return -1;
        if ( pa->rec_type > pb->rec_type ) return +1;
        // else equal
        return strncmp( pa->cust_code, pb->cust_code,5);
    }

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    As salem says, your union is the length of your biggest structure. The thing is that the fields you require to sort - the record type and the customer code - are common to each structure and no matter what the record, they hold the same position when your file is read in to memory. To acheive this, you will need to allocate memory for the number of records on file.

    Fellow CIL student here...mail me, I sent you an e-mail.

    to calculate the number of records on file...if your file is binary use fseek() and ftell() - remember to rewind the file pointerafterwards.

    to read the file into memory, use 2 memory pointers and malloc. One pointer remains at the start of the allocated memory, the other is used to access the records.

    call your qsort function.

    once sorted read the records from memory to the sorted file.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  5. #5
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    Thanks fellas, I will try out your recommendations and get back to you!

  6. #6
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167

    I just thought.........................

    I have just thought......I have to write a function/loop to count the number of records BEFORE i can read them into an array of unions, so how can I then set the size of an array of unions before I know the numbwer of records?

  7. #7
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    I know that it has more than 100 records, so would I be able to resize the array of unions to(say)200?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM