Need help using rewind() in C working with files

64 Views Asked by At

This is my code:

#include <stdio.h>

void correctNumbers(FILE* input, FILE* output) {
    int year, month, day;
    int amount;

    while (fscanf(input, "%d-%d-%d,%d", &year, &month, &day, &amount) == 4) {

        if (amount < 0) {
            amount = 0;
        }

        fprintf(output, "%d-%d-%d,%d\n", year, month, day, amount);
    }
}

double averageValue(FILE* output){
    double sum = 0; 
    double total = 0;
    int year, month, day, amount;
    
    /*fclose(output);
    output = fopen("corrected.txt", "r");*/
 
    while(fscanf(output, "%d-%d-%d,%d", &year, &month, &day, &amount) == 4){
        total++;
        sum += amount;
    }
    printf("Average value: %.2lf\n", sum/total);
    return sum/total;
}

void maxVisit(FILE* output) {
    int year, month, day, amount;
    int maxDay;  
    int maxVisitors = -1; 
    
    /*fclose(output);
    output = fopen("korrigiert.txt", "r");*/

    while (fscanf(output, "%d-%d-%d,%d", &year, &month, &day, &amount) == 4) {
        if (amount >= maxVisitors) {
           
            maxDay = day;
            maxVisitors = amount;
        }
    }
    printf("Day with the highest amount of visitors: %d\n", maxDay);
}


int main(int argc, char* argv[]){
    FILE* input;
    FILE* output;

    if(argc < 2){
        printf("Pls include a filename\n");
        return 0;
    }else{
        input = fopen(argv[1], "r");
        output = fopen("corrected.txt", "w");
        if(input == NULL || output == NULL){
            printf("File doesnt exist or failed to open\n");
            return 0;
        }
    }

    correctNumbers(input, output);
    rewind(output);
    averageValue(output);
    rewind(output);
    maxVisit(output);

    fclose(input);
    fclose(output);

    return 0;
}

What each function is supposed to do:

correctNumbers = corrects the negative numbers in a given file to 0 and write them in a new file called "corrected.txt" 
averageValue = calculates the averageValue of the visitors (the last number in the given file on each date) in corrected.txt.
maxVisit = calculates the day that had the highest amounts of visitors in corrected.txt.
main = give the main function a file that needs to be corrected. Do correction, averagevalue, maxvisitor. Close the files.

All of the functions are doing what they are supposed to.

The given file looks like this: (the last number being the amount of visitors)

2021-10-05,-3
2021-10-06,-2
2022-02-02,7
2022-02-03,10
2022-02-04,9

The corrected file will look like this:

2021-10-5,0
2021-10-6,0
2022-2-2,7
2022-2-3,10
2022-2-4,9

If the program works it will put out:

Average value: 4.50
Day with the highest amount of visitors: 3

The way the code is right now i get this:

Average value: -nan
Day with the highest amount of visitors: 0

I put some printf commands in between the lines and found out that the while loops dont work if i use rewind() like this.

But for a task, i have to use rewind() in the main function. Can anyone help me do it right?

Now my problem is that the way i sent this program it doesn't work because my use of rewind() is somehow wrong is my guess but i don't understand why. Because if i enable the comments that i put into the functions averageValue and maxVisitor and disable the rewind() lines the program works perfectly fine

1

There are 1 best solutions below

1
R0gue On

@Weather Vane oh wow opening the file with w+ fixed the problem ! Ill need to do my research on w+ and r+ then and see what is actually different. Thank you very much