Thread: Can anyone help with sorting problem???

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

    Unhappy Can anyone help with sorting problem???

    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 cust_code[6];
    char rec_type;
    };

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

    struct IR_record
    {
    char cust_code[6];
    char rec_type;
    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 (ALLRECORDS)))==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(ALLRECORDS)
    ,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(strncmp((char*)arg1,(char*)arg2,5));
    }


    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, I shall reply to your last e-mail tonite. One problem I noticed is that you have only created one pointer to memory. It is easier if you have the output file in program one in binary. Tell more later.
    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. Sorting problem?
    By audinue in forum C Programming
    Replies: 5
    Last Post: 01-06-2009, 02:16 PM
  2. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  3. What is problem about the sorting?
    By Mathsniper in forum C Programming
    Replies: 2
    Last Post: 04-17-2005, 07:00 AM
  4. Sorting text file problem...
    By John-m in forum C Programming
    Replies: 3
    Last Post: 10-01-2002, 04:51 PM
  5. Sorting array output problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 01:44 PM