0

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();
}
Kemmy
  • 1
  • 1
  • 1
    First please take some time to read [Why is “while( !feof(file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – Some programmer dude Nov 10 '22 at 09:25
  • `fflush(stdout);` should go before `scanf`. – Gerhardh Nov 10 '22 at 09:26
  • 1
    If you require the user to input the line number the user must know the content of the file. As you want to avoid this, you need to use some different way to identify the entry to be modified. Either use email or name of the user to be modified or introduce some unique feature like an ID. – Gerhardh Nov 10 '22 at 09:28
  • As for your problem, please take some time to learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs. More specifically, how to use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through your code line by line (while monitoring variables and their values) to see what's happening. – Some programmer dude Nov 10 '22 at 09:28
  • OT: Read this https://stackoverflow.com/q/5431941/4386427 about `while (!feof(cd1))` – Support Ukraine Nov 10 '22 at 09:40

0 Answers0