The background is the following: I search for an ID I want to replace and then I look at my file MedicalStore.txt for it. If I find it I replace it with another line of or record that didn't previously exist in the file. I make up another temporary file and copy-paste all the data with the exception of the searched ID which I replace using an If condition. I will attach the file as well.
Modify(int SiD){
struct customerinfo{
char Prefix[20];
char Name[20];
int ID;
unsigned long int Pnum;
};
struct customerinfo customer;
FILE * Fptr;
FILE * Ftemp;
Fptr = fopen("MedicalStore.txt","r");
Ftemp = fopen("replace.txt","w");
char singleLine[150],newline[150],prefix[10],name[20];
int id,c=0;
unsigned long int num;
while (!feof(Fptr)){
fgets(singleLine,150,Fptr);
c++;
sscanf(singleLine,"%s %s %d %d\n",prefix,name,&id,&num);
//printf("%s %s %d %d\n",prefix,name,id,num);
if (id == SiD){
strcpy(customer.Prefix,"Customer");
printf("Enter Customer Name:\n");
fflush(stdin);
gets(customer.Name);
printf("Enter unique ID of Customer : ");
scanf("%d",&customer.ID);
printf("Enter phone number of customer : ");
scanf("%d",&customer.Pnum);
printf("%d",customer.Pnum);
sprintf_s(newline,150, "%s %s %d %d\n",customer.Prefix,customer.Name,customer.ID,customer.Pnum);
fputs(newline,Ftemp);
} else {
fputs(singleLine,Ftemp);
}
}
fclose(Fptr);
fclose(Ftemp);
remove("MedicalStore.txt");
rename("replace.txt","MedicalStore.txt");
return 0;
}
Before editing with the code I replaced the 2nd line with another record