Thread: sorting union of structures with q sort

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    Unhappy HELP NEEDED WITH QSORT() FUNCTION!!

    hi, hope someone can shed some light on this for me, I'm a bit stumped at the moment.

    Writing a prog to read records from a binary file into memory using malloc() thenh sort them using q sort.

    record structures are as follows.......

    struct ir_record {
    char rec_type;
    char cust_code[6];
    char p_num[7];
    char ir_quantity[5];
    };

    struct d_record {
    char rec_type;
    char cust_code[6];
    };

    struct c_record {
    char rec_type;
    char cust_code[6];
    char cust_name[21];
    char cust_addr[61];
    char cust_bal[10];
    char limit[8];
    };

    union records {
    struct ir_record issue_receipt;
    struct d_record deletion;
    struct c_record creation;
    };

    the records must be sorted first by cust_code and then any with the same code must be sorted so that the c_record comes last.

    can anyone show me how to write the 2 comparison functions needed to perfrom this sort????



    Colin.
    Last edited by colw; 03-15-2002 at 02:14 PM.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    I'll show you the answer to this, if you show me how to test for a missing name field in P1, fellow Computeach student!

    I've recently sent off both P1 and P2, I know P2 works from my own test data and P1 works fine except for the last invalid record of Computeach's 'secret' td.dat.

    ie all my checks for check digits, non-numerics etc are fine, but I am stumped on the 'missing name' thing.

    I've tried counting the record size, checking for spaces etc to no avail and am by now pretty p***ed off with the whole thing, even a clue as to where to llok in the coursework would help, but I'm convinced there's nothing in there close to this.


    cheers

    foffo
    'C that?... I felt nowt' - The Original Foffo Spearjig and his hard dog. The Tube TV show, sometime in the 80's

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi, fellow compu student here, with refernce to the "secret" td of computeach, what would that be by chance. Is it zenctd.dat you are on about. and which record in particular. If you have this secret test data could you e-mail it or point me to a place it is on web. Then maybe I can help you.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    4
    must say I'm a bit in the dark about this one too as i don't think there's anything secret about the test data.

    Like you think it refers to ZENTD.DAT

    As for the record it's no 139 or 140 "C10006"


  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    HI COWL,
    just sent you an e-mail regarding your problem with qsorts, souce code is attached for a little demo program to illustrate the use of sorts on structs and unions. Hope it helps.

    Foffo Spearjig,
    the route of your problem may lie in testing for the new line character. In my coding I test every character in every field for the "\n". Resulting error means one or more fields are missing.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    2
    I'm also have the same problem of sorting the file by code and then type so that C type comes last. Did you manage to find out the solution.

    Also if any one require the test data zentd.dat, then I do have a copy

    Thanks

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    Lightbulb qsort

    I've solved this problem with a little help so if you need any advice drop me a private message.

    cheers C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. sorting an array of structures
    By kisiellll in forum C Programming
    Replies: 15
    Last Post: 04-06-2009, 05:25 AM
  3. Selection sort, not sorting
    By swgh in forum C++ Programming
    Replies: 10
    Last Post: 04-23-2007, 11:17 AM
  4. best sorting method to sort half end of array
    By nitinmhetre in forum C Programming
    Replies: 2
    Last Post: 01-11-2007, 12:18 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM