ok i have read the data in from one file and there is a second file that has some more info and i am going to read one piece in at a time and then do some things with it according to exactly what it is. So my question is this: How am i reading in the second file? am i starting another linked list or is it going in the same list? Also say i want to compare the SSN from teh first list with one from the second list to see if the customer is already on that list how could i do that? My problem is in the main function.
Code:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial;
char socialnum[10];
double balance;
double amount;
struct customer *next;
}CUSTOMER;
void addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
CUSTOMER *tempptr2;
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) && (strcmp(tempptr->fname,temprecord->fname)>0)) // 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;
strcpy(tempptr->socialnum,temprecord->socialnum);
tempptr->balance = temprecord->balance;
}
void addtolist2(CUSTOMER **transactions,CUSTOMER *temp)
{
CUSTOMER *tempptr3;
CUSTOMER *tempptr4;
if(*transactions==NULL)
{
*transactions = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr3 = *transactions;
tempptr3->next=NULL;
}
else
{
tempptr3 = *transactions;
while((tempptr3->next!=NULL) && (strcmp(tempptr3->fname,temp->fname)<0))
tempptr3=tempptr3->next;
if((*transactions==tempptr3) && (strcmp(tempptr3->fname,temp->fname)>0))
{
tempptr4=tempptr3;
*transactions = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr3 = *transactions;
}
else
{
tempptr4=tempptr3->next;
tempptr3->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr3=tempptr3->next;
}
tempptr3->next=tempptr4;
}
strcpy(tempptr3->fname,temp->fname);
strcpy(tempptr3->lname,temp->lname);
tempptr3->initial = temp->initial;
strcpy(tempptr3->socialnum,temp->socialnum);
tempptr3->balance = temp->balance;
}
void printlist(CUSTOMER **bankrecords)
{
CUSTOMER *tempptr;
int counter=1;
tempptr = *bankrecords;
printf("NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT\n");
printf("----------------------------------------------------------------\n\n");
while(tempptr!=NULL)
{
printf("%d %5X %-12s%-9s%c %s %.2lf ",counter,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance);
if(tempptr->next==NULL)
printf(" NULL\n");
else
printf("%5X\n",tempptr->next);
tempptr=tempptr->next;
counter++;
}
printf("\n\n");
}
int main()
{
FILE *infile,*infile2;
CUSTOMER *bankrecords;
CUSTOMER temprecord;
CUSTOMER *transactions;
CUSTOMER temp;
bankrecords=NULL;
infile=fopen("data.dat","r");
if(infile == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
// READ IN FILE TO THE LIST
while(!feof(infile))
{
fscanf(infile,"%s %s %c %s %lf",temprecord.lname,temprecord.fname,&(temprecord.initial),temprecord.socialnum,&(temprecord.balance));
addtolist(&bankrecords,&temprecord);
printlist(&bankrecords);
}
}
infile2=fopen("data_2.dat","r");
/////This is the part in Question!!!!///
if(infile2 == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
while(!feof(infile2))
{
fscanf(infile,"%s %s %c %s %lf",temp.lname,temp.fname,&(temp.initial),temp.socialnum,&(temp.amount));
}
}
return 0;
}