while condition question..
a cellphone company updates its data in a daily basis.
the main file of all customers if updated till yesterday
each line in this file has
20 chars name
9 chars Social security number
10 chars cell phone number
20 chars address
2 chars type of customer
8 chars date when the customer left the company
the file is sorted by cellphone number in ascended order.
each day the company makes another two files
one file is the file of new customers or present customers which changed their type.
regarding first file:
the first line is the date of the last update on this file.
20 chars name
9 chars Social security number
10 chars cell phone number
20 chars address
2 chars type of customer
8 chars date when the customer left the company
10 chars the name who wrote the update
the file is sorted by cellphone number in ascending order.
the second file is the list of people who left the company.
regarding the second file:
the first line is the date of the last update on this file.
10 chars cell phone number
10 chars the name of the man who wrote the update
the file is sorted by cellphone number in ascending order.
all the fields are sticked together without any space between them.
write a function which which takes the 3 files and creates a new file
which in the new main file.
why they say while(flagOld==3 || flagNew==3)
??
Code:
void update(char *filename, FILE* fp1, FILE* fp2, char* newfilename)
{
FILE *fOld, *fNew;
int flagOld, flagNew, flagDel;
char details1Old[39],details1New[30],telOld[11],telNew[11],telDel[11],details2Old[31],details2New[31],dateNew[9];
if(!((fOld = fopen(filename, "r"))&&(fNew = fopen(newfilename,"w"))))
{
puts(" The program cannot open a file");
exit(1);
}
flagOld = fscanf(fOld,"%29[^$]%10s%38[^$]%*c",details1Old,telOld,details2Old);
fscanf(fp1,"%s%*c",dateNew);
flagNew= scanf(fp1,"%29[^$]%10s%30[^$]%11*c",details1New,telNew,details2New);
fscanf(fp2,"%9*c"); // Update date
flagDel= fscanf(fp2,"%10s%*11c",telDel);
while(flagOld==3 || flagNew==3)
{
if(flagOld < 3 || (flagNew==3 && strcmp(telNew, telOld)<0)){ // New number
fprintf(fNew,"%s%s%s%s\n",details1New,telNew,details2New,dateNew);
flagNew= fscanf(fp1,"%29[^$]%10s%30[^$]%*11c",details1New,telNew,details2New);
continue;
}
if(flagDel==1 && !strcmp(telOld,telDel))
{ // Number to delete
flagOld = fscanf(fOld,"%29[^$]%10s%38[^$]%*c",details1Old,telOld,details2Old);
flagDel= fscanf(fp2,"%10s%*11c",telDel);
continue;
}
if(flagNew < 3 || strcmp(telOld, telNew)<0){ // No change for this number
fprintf(fNew,"%s%s%s\n", details1Old,telOld,details2Old);
flagOld = fscanf(fOld,"%29[^$]%10s%38[^$]%*c",details1Old,telOld,details2Old);
continue;
}
fprintf(fNew,"%s%s%s%s\n",details1New,telNew,details2New,dateNew); // Change of date
flagOld = fscanf(fOld,"%29[^$]%10s%38[^$]%*c",details1Old,telOld,details2Old);
flagNew= fscanf(fp1,"%29[^$]%10s%30[^$]%*11c",details1New,telNew,details2New);
}
if(fclose(fOld)==EOF || fclose(fNew)==EOF)
{
puts(" The program cannot close a file");
exit(1);
}
}