I want to make a function of updating customer's personal information in my simple ordering system, I can't figure out the way to let the customer change their details without them having the access to see other customers' personal information because my code now is printing out the whole text file content to let user choose which line they would like to update. Please help me :(
p/s: I used this way to make the function of updating menu item in my ordering system and they work well for that part, however because I am really new to programming I can't figure out a better other way to improve on this part.
Below is the current function code that I am currently stuck on:
void ChgDeet()
{
FILE *cd1;
FILE *cd2;
int line = 0;
int temp = 1;
int flag = 0;
char fname[50];
char lname[50];
char email[250];
int number;
char d;
printf("\nNAME\t\tEMAIL\t\t\tPHONE NUMBER\n");
cd1 = fopen("custdeet.txt", "r"); // open file in read mode
if (cd1 == NULL){
printf("\nUnable to open file.\n");
quit();
}
while (!feof(cd1)){
d = getc(cd1);
printf("%c",d);
}
//take cd1 to start point
rewind(cd1);
printf("\n\nEnter line number to be updated: ");
scanf("%d", &line);
fflush(stdout);
//open duplicated file in write mode
cd2 = fopen("copy.txt", "w");
while (!feof(cd1)){
d = getc(cd1);
if (d == '\n')
temp++;
//until the line to be deleted comes, copy content from one file to the other
if (temp != line){
putc(d, cd2);
}
if (flag == 0 && temp == line){
printf("\nUpdated First Name: ");
scanf("%s",&fname);
printf("Updated Last Name: ");
scanf("%s",&lname);
printf("Updated Email: ");
scanf("%s",&email);
printf ("Updated Phone Number (+60): ");
scanf("%d",&number);
fprintf(cd2, "\n%s\t%s\t%s\t+60%d", fname,lname,email,number);
flag = 1;
}
}
fclose(cd1);
fclose(cd2);
remove("custdeet.txt");
rename("copy.txt","custdeet.txt");
printf("\nUpdate Menu: \n");
cd1 = fopen("custdeet.txt", "r");
if (cd1 == NULL){
printf("\n Unable to open file.\n");
quit();
}
while (!feof(cd1)) {
d = getc(cd1);
printf("%c", d);
}
fclose(cd1);
printf("\nYour personal information updated!");
printf("\n...redirecting you back to homepage...");
CustMenu();
}