ok the easiest way to do that is probably to sort it as its being inserted into the list and then just print them out as normal, below is the code i posted which your using now but modified which sorts the list on insertion:
Code:
#include <stdio.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial;
long socialnum;
double balance;
struct customer *next;
}CUSTOMER;
int main()
{
FILE *infile;
CUSTOMER *bankrecords;
CUSTOMER *tempptr;
CUSTOMER *tempptr2;
CUSTOMER temprecord;
bankrecords=NULL;
infile=fopen("data.dat","r");
if(infile == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
// READ IN ALL THE FILE INTO THE LIST
while(!feof(infile))
{
fscanf(infile,"%s %s %c %D %lf",temprecord.fname,temprecord.lname,&(temprecord.initial),&(temprecord.socialnum),&(temprecord.balance));
if(bankrecords==NULL)
{
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = bankrecords;
tempptr->next=NULL;
}
else // RECORDS ALREADY EXIST
{
tempptr = bankrecords;
while((tempptr->next!=NULL) && (strcmp(tempptr->fname,temprecord.fname)<0))
tempptr=tempptr->next;
if(bankrecords==tempptr) // RECORD BELONGS AT START OF LIST
{
tempptr2=tempptr;
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = bankrecords;
}
else // RECORD IS SOMEWHERE IN MIDDLE OR END
{
tempptr2=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
tempptr->next=tempptr2;
}
//COPY ALL DATA FROM TEMP RECORD TO NEW ONE
strcpy(tempptr->fname,temprecord.fname);
strcpy(tempptr->lname,temprecord.lname);
tempptr->initial = temprecord.initial;
tempptr->socialnum = temprecord.socialnum;
tempptr->balance = temprecord.balance;
}
fclose(infile);
// PRINT ALL THE LIST
tempptr = bankrecords;
printf("NUM ADDRESS LAST\tFIRST MI\tSSN BALANCE NEXT\n");
printf("-----------------------------------------------------\n\n");
while(tempptr!=NULL)
{
printf("%s %c %s %ld %.2lf\n",tempptr->fname,tempptr->initial,tempptr->lname,tempptr->socialnum,tempptr->balance);
tempptr=tempptr->next;
}
}
return 1;
}
you can either cut bits out or create 2 new variables as:
Code:
CUSTOMER *tempptr2;
CUSTOMER temprecord;
and then change the code within the while(!feof(infile)) to
Code:
while(!feof(infile))
{
fscanf(infile,"%s %s %c %D %lf",temprecord.fname,temprecord.lname,&(temprecord.initial),&(temprecord.socialnum),&(temprecord.balance));
if(bankrecords==NULL)
{
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = bankrecords;
tempptr->next=NULL;
}
else // RECORDS ALREADY EXIST
{
tempptr = bankrecords;
while((tempptr->next!=NULL) && (strcmp(tempptr->fname,temprecord.fname)<0))
tempptr=tempptr->next;
if(bankrecords==tempptr) // RECORD BELONGS AT START OF LIST
{
tempptr2=tempptr;
bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = bankrecords;
}
else // RECORD IS SOMEWHERE IN MIDDLE OR END
{
tempptr2=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
tempptr->next=tempptr2;
}
//COPY ALL DATA FROM TEMP RECORD TO NEW ONE
strcpy(tempptr->fname,temprecord.fname);
strcpy(tempptr->lname,temprecord.lname);
tempptr->initial = temprecord.initial;
tempptr->socialnum = temprecord.socialnum;
tempptr->balance = temprecord.balance;
}
hope this helps