Thread: Need help with qsort() and union of structures

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    5

    Unhappy Need help with qsort() and union of structures

    I am writing a program that reads in a file containing three different types of record, a creation rec, a insertion rec, and a deletion rec. I need allocate memory and then read each record into the allocated memory. These records then need to be sorted into order of customer reference code using qsort.

    I have got as far as allocating the the memory and reading in the records into allocated memory(i think i have done it right). I am now stuck as to how to sort the records into order of customer reference code. Could someone please explain to me how to use qsort to do this

    My code so far is:

    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<ctype.h>
    #include<time.h>
    #include<string.h>


    #define SPACE ' '
    #define TRUE 1
    #define FALSE 0

    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_add[61];
    char cust_bal[10];
    char limit[8];
    };

    struct IR_record
    {
    char rec_type;
    char cust_code[6];
    char part_num[7];
    char issue[5];
    };

    typedef union records
    {
    struct IR_record ir;
    struct C_record creation;
    struct D_record deletion;
    }ALLRECORDS;

    int sort_func(const void *arg1,const void *arg2);


    void main()
    {
    FILE *vf_ptr,*sd_ptr,*prn_ptr;
    int rec_count=0;
    char ch;
    ALLRECORDS *mem_ptr;

    if((vf_ptr=fopen("503353VF.txt","r"))==NULL)
    {
    printf("Sorry, unable to locate the file calleed 503353VF.txt\n");
    printf("Press space to exit program\n");
    while(getch()!=' ')
    printf("Press space to exit!!!!!!\n");
    exit(1);
    }

    if((sd_ptr=fopen("503353SD.txt","w"))==NULL)
    {
    printf("Sorry, unable to locate the file calleed 503353SD.DAT\n");
    printf("Press space to exit program\n");
    while(getch()!=' ')
    printf("Press space to exit!!!!!!\n");
    exit(1);
    }

    if((prn_ptr=fopen("PRN","w"))==NULL)
    {
    printf("Sorry, unable to locate the printer\n");
    printf("Press space to exit program\n");
    while(getch()!=' ')
    printf("Press space to exit!!!!!!\n");
    exit(1);
    }


    while((ch=fgetc(vf_ptr))!=EOF)
    if(ch=='\n')
    rec_count++;
    fseek(vf_ptr,0,SEEK_SET);

    if((mem_ptr=(ALLRECORDS*)calloc(rec_count,sizeof(A LLRECORDS)))==NULL)
    {
    printf("Sorry - Out of memory!!!!!!!!");
    while(getch()!=SPACE)
    printf("\nPress space to continue");
    }

    /*Read in the file to temp memory storage*/
    while((ch=getc(vf_ptr))!=EOF)
    {
    fseek(vf_ptr,ftell(vf_ptr)-(1*sizeof(char)),SEEK_SET);
    switch(ch)
    {
    case 'C' : fscanf(vf_ptr,"%c",mem_ptr->creation.rec_type);
    fgets(mem_ptr->creation.cust_code,6,vf_ptr);
    fgets(mem_ptr->creation.cust_name,21,vf_ptr);
    fgets(mem_ptr->creation.cust_add,61,vf_ptr);
    fgets(mem_ptr->creation.cust_bal,10,vf_ptr);
    fgets(mem_ptr->creation.limit,8,vf_ptr);
    fscanf(vf_ptr,"\n");
    break;
    case 'I' :
    case 'R' : fscanf(vf_ptr,"%c",mem_ptr->ir.rec_type);
    fgets(mem_ptr->ir.cust_code,6,vf_ptr);
    fgets(mem_ptr->ir.part_num,7,vf_ptr);
    fgets(mem_ptr->ir.issue,5,vf_ptr);
    fscanf(vf_ptr,"\n");
    break;
    case 'D' : fscanf(vf_ptr,"%c",mem_ptr->deletion.rec_type);
    fgets(mem_ptr->deletion.cust_code,6,vf_ptr);
    fscanf(vf_ptr,"\n");
    break;
    default : break;
    }
    }

    qsort((void*)mem_ptr,rec_count,sizeof(mem_ptr[0]),sort_func);

    free(mem_ptr);


    fclose(vf_ptr);
    fclose(sd_ptr);
    fclose(prn_ptr);
    }

    /*----------------------------------------------------------------------------*/
    /* FUNCTION DEFINITIONS */
    /*----------------------------------------------------------------------------*/

    int sort_func(const void *arg1,const void *arg2)
    {
    return(strcmp((char*)arg1,(char*)arg2));
    }


    Once the records are in customer ref code order i have to save them back to a new file. Can someone please help me with this.
    Hope it's not too much trouble
    Cheers
    Dan

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi Dan,
    reading this at work, will answer later tonite. Meanwhile I think you should think about writing the output file in program 1 in binary mode. This makes program 2 easier to manipulate.

    [email protected]
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. qsort and unions
    By doylie in forum C Programming
    Replies: 1
    Last Post: 01-31-2003, 12:13 PM
  2. help with c project using qsort
    By VegasSte in forum C Programming
    Replies: 6
    Last Post: 10-02-2002, 03:19 AM
  3. sorting union of structures with q sort
    By colw in forum C Programming
    Replies: 6
    Last Post: 04-09-2002, 09:51 AM
  4. Using malloc() & qsort with a union revisited
    By Cyber Kitten in forum C Programming
    Replies: 11
    Last Post: 11-24-2001, 06:07 PM
  5. Using malloc() & qsort with a union
    By Cyber Kitten in forum C Programming
    Replies: 2
    Last Post: 09-02-2001, 07:57 AM