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