Question about the output of the file input/output function

Asked 2 years ago, Updated 2 years ago, 46 views

File Input/Output Function Question

Hello, I'm taking the liberty of asking questions to others first The program is to input students' information through the file input/output function and get total scores and averages I have a question about the program output being good, but the output value is weird. There's nothing wrong with creating a new file, saving and loading the contents However, it seems that the output of the average part comes up to the "-1" part, which should be finished. There is no -1 part in the notepad, but when you run the source code, you add up to the last 77 points I think it's because the contents of the buffer remain when I input it, so the result is the same even though I initialized the buffer. I want to know where I made a mistake. I'd appreciate it if you let me know

Results screen

![Image][1]

![Image][2]

Source Code

///* Practice) Writing a grade program */
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>  
#include <stdlib.h>

void main() {
    // File = variable
    FILE* fp = NULL;
    int number, count = 0;
    char name[20], fname[30];
    float score, total = 0.0 ;


    // Get transcript file name
    printf("Enter your grade file name >>");
    scanf("%s", fname);
    fflush(stdin); // buffer initialization

    // Creating a Sexual File
    if ((fp = fopen(fname, "w")) == NULL) {
        fprintf(stderr, "Cannot open file %s", fname);
        exit(1);
    }

    // sexual content output
    while (1) {
        printf("Enter your grade for the class name (exits when you enter a negative number) >> ");
        scanf("%d", &number);
        if (number < 0) 
            break;
        scanf("%s %f", name, &score);
        fflush(stdin);

        // Formatted Output
        fprintf(fp, "%d %s %f\n", number, name, score);
    }
    fclose(fp); // If the end function is not entered here, the mean is not obtained

    // Failed to open file
    if ((fp = fopen(fname, "r")) == NULL) {
        fprintf (stderr, "%s file cannot be opened", fname);
        exit(1);
    }

    // the sum of grades
    while (!feof(fp)) {
        fscanf(fp, "%d %s %f", &number, name, &score);
        total += score;
            count++;
    }
    printf("%d student's total score = %f\n", count, total);
    printf("%d student average score = %f\n", count, total / count);

    // File Shutdown
    fclose(fp);

}

[1]: https://res.cloudinary.com/eightcruz/image/upload/v1606902192/hl12gbtiubxmxnzz4vm4.png [2][2]: https://res.cloudinary.com/eightcruz/image/upload/v1606902206/qvza79o8huyi1by8iwzm.png

visual-studio

2022-09-20 19:25

2 Answers

Please refer to the code below.

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>  
#include <stdlib.h>
void main() {
    // File = variable
    FILE* fp = NULL;

    int number = 0, count = 0;
    char name[80] = { 0, }, fname[80] = { 0, };
    float score = 0.0, total = 0.0;

    // Get transcript file name
    printf("Enter your grade file name >>");
    scanf("%s", fname);

    // Creating a Sexual File
    if ((fp = fopen(fname, "w")) == NULL) {
        fprintf(stderr, "Cannot open file %s", fname);
        exit(1);
    }

    // sexual content output
    while (1) {
        printf("Enter your grade for the class name (exits when you enter a negative number) >> ");
        scanf("%d", &number);
        if (number < 0)
            break;
        scanf("%s %f", name, &score);
        fflush(stdin);

        // Formatted Output
        fprintf(fp, "%d %s %f\n", number, name, score);
    }
    fclose(fp); // If the end function is not entered here, the mean is not obtained

    // Failed to open file
    if ((fp = fopen(fname, "r")) == NULL) {
        fprintf (stderr, "%s file cannot be opened", fname);
        exit(1);
    }

    // the sum of grades
    while (fscanf(fp, "%d %s %f", &number, name, &score) == 3)
    {
        total += score;
        count++;
    }
    printf("%d student's total score = %f\n", count, total);
    printf("%d student average score = %f\n", count, total / count);

    // File Shutdown
    fclose(fp);

    return 0;

}


2022-09-20 19:25

Thank you. It was helpful


2022-09-20 19:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.