Home > front end >  Cannot understand why fprintf() doesn't work here
Cannot understand why fprintf() doesn't work here

Time:06-22

Cannot understand fprintf(). I need to rewrite data in .txt file but it doesn't work. Problem is in void fileEdit function. After I write new number for 'wT', 'wF' or 'nap' it saves only in while (whT) loop and doesn't want to go further. Full code with .txt file

void fileEdit(FILE **file) {
    int dayEdit = 0;
    int playerChoose = 0;
    float nap = 0.0, wF = 0.0, wT = 0.0;
    while (true) {
        int day = 0;
        bool whT = true;
        system("CLS");
        printFile(file);
        printf("Which day you want to edit[1 - 31, 0 to exit]: ");
        scanf_s("%d", &dayEdit);
        if (dayEdit == 0)
            break;
        while (day < dayEdit) {
            fscanf_s(*file, "%d %f %f %f", &day, &wF, &wT, &nap);
            printf("\n %d ", ftell(*file));
        }
        while (whT) {
            system("CLS");
            printf(" \n\n -------------------------------\n|      Job Hours Managment     
 |\n -------------------------------\n| Day |  From  |  Till  |  Nap  |");
            printf("\n -------------------------------\n| = | %6.1f | %6.1f | %5.1f |", day, wF, wT, nap);
            printf("\n");
            printf(" -------------------------------\n");
            printf("\nWhat you want to edit?[1 From, 2 Till, 3 Nap, 4 Save]: ");
            scanf_s("%d", &playerChoose);
            switch (playerChoose) {
            case 1:
                printf("\nWorked from: ");
                scanf_s("%f", &wF);
                break;
            case 2:
                printf("\nWorked till: ");
                scanf_s("%f", &wT);
                break;
            case 3:
                printf("\nNap: ");
                scanf_s("%f", &nap);
                break;
            case 4:
                whT = false;
            default:
                break;
            }
        }
        fprintf(*file, "%d %f %f %f\n", day, wF, wT, nap); // <---- Here i want to save new data
    }
}

CodePudding user response:

Your approach is not appropriate for text files: rewriting some data in the middle of the file does not insert the data, it overwrites whatever contents is already there and text conversion of numbers typically may use more or fewer digits than existing contents, so the rest of the file is either corrupted or leaves some stray bytes that prevent proper parsing.

There are different options:

  • you can load the whole file in memory, update the contents in memory, and write the updated file version as a new file, then if writing completed without errors, the old file can be removed and the new file can be renamed to the old name.

  • you can copy the old file to a new file, updating appropriate contents on the fly, and perform the same renaming scheme at the end

  • you can use a fixed length file format, most likely a binary format, and update only the parts that need to be changed. To switch from reading to writing and vice versa to and from the same FILE *, you must issue a positioning request: fseek() or rewind(). To write the updated contents, you must set the file position appropriately at the start of the target area.

  • Related