Input/output of transcript program files

Asked 2 years ago, Updated 2 years ago, 110 views

It's a grade processing program. You want to add file input/output from code created with functions and pointers. In the 'Grades Processing Program.txt' file

1111 Gaga 10099897

2222 Nana 90898887

reach 3333 80 79 78 77

I wrote four grades, name, and score in three lines. And then

#define_CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define SUB 4
char subname[SUB][6] = { "Korean", "English", "Mathematics", "C language"};
struct STUDENT {
    char id[5];
    char name[7];
    int score[SUB + 1];
    float avg;
    char alpha;
    int rank;
};
void input(struct STUDENT *stu, int PEOPLE) {
    FILE *fp;
    fp = fopen ("Sex Processing Program.txt", "w+");
    //char buffer[50];

    for (int i = 0; i < PEOPLE; i++) {
        fscanf(fp, "%s %s %d %d %d %d", (stu + i)->id, (stu + i)->name, &(stu+i)->score[0], &(stu + i)->score[1], &(stu + i)->score[2], &(stu + i)->score[3]);
        //fscanf(fp, "%s %s %d %d %d %d", stu[i].id, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2], &stu[i].score[3]);
    }

    /*for (int i = 0; i < PEOPLE; i++) {
        fscanf(fp, "%s\n", (stu + i)->id);
        fscanf(fp, "%s\n", (stu + i)->name);
        for (int j = 0; j < SUB; j++) {
            fscanf(fp, "%d\n", &(stu + i)->score[j]);
        }
    }*/

    fclose(fp);
} // Input function
void calculation(struct STUDENT *stu, int PEOPLE) {
    for (int i = 0; i < PEOPLE; i++) {
        (stu + i)->score[SUB] = 0;
        for (int j = 0; j < SUB; j++) {
            (stu + i)->score[SUB] += (stu + i)->score[j];
        } //Total score
        (stu + i)->avg = (float) (stu + i)->score[SUB] // mean
    }
} // Calculation function
void line() {
    for (int i = 0; i < 75; i++) {
        printf("-");
    }
    printf("\n");
}
void print(struct STUDENT *stu, int PEOPLE) {
    printf("\n");
    line();
    printf ("Grade Name: Korean, English, Mathematics, C Language Total Score Average Ranking\n");
    //fprintf(fp, "Grade Name, Korean, English, Mathematics, c Language Total Score Average Ranking\n");
    line();
    for (int i = 0; i < PEOPLE; i++) {
        printf("%6s %8s", (stu + i)->id, (stu + i)->name);
        //fprintf(fp, "%6s %8s", (stu + i)->id, (stu + i)->name);
        for (int j = 0; j < SUB; j++) {
            printf("%7d", (stu + i)->score[j]);
            //fprintf(fp, "%7d", (stu + i)->score[j]);
        }
        printf("%8d  %7.2f %5c  %4d\n", (stu + i)->score[SUB], (stu + i)->avg, (stu + i)->alpha, (stu + i)->rank);
        //fprintf(fp, "%8d  %7.2f %5c  %4d\n", (stu + i)->score[SUB], (stu + i)->avg, (stu + i)->alpha, (stu + i)->rank);
    }
    line();
} //output function
int main() {
    int PEOPLE;
    int ban_total[SUB + 1] = { 0 };
    double ban_avg[SUB + 1] = { 0 };

    printf ("How many people do you want to enter?" ");
    scanf("%d", &PEOPLE);
    struct STUDENT *stu = (struct STUDENT *)malloc(sizeof(struct STUDENT)*PEOPLE);

    input(stu,PEOPLE); //Enter
    calculation (stu, PEOPLE); //calculation

    for (int i = 0; i < SUB; i++) {
        for (int j = 0; j < PEOPLE; j++) {
            ban_total[i] += (stu + j)->score[i];
        } //Total score of subjects
        ban_avg[i] = (float)ban_total[i] /PEOPLE; //subject mean
    }

    for (int i = 0; i < PEOPLE; i++) {
        switch ((int)(stu + i)->avg / 10) {
        case 10: case 9: (stu + i)->alpha = 'A'; break;
        case 8: (stu + i)->alpha = 'B'; break;
        case 7: (stu + i)->alpha = 'C'; break;
        case 6: (stu + i)->alpha = 'D'; break;
        default: (stu + i)->alpha = 'F'; break;
        } //Grade
    }
    for (int i = 0; i < PEOPLE; i++) {
        (stu + i)->rank = 1;
    } //Initialize to seat 1
    for (int i = 0; i < PEOPLE; i++) {
        for (int j = i + 1; j < PEOPLE; j++) {
            if ((stu + i)->avg < (stu + j)->avg) {
                (stu + i)->rank++;
            }
            else {
                (stu + j)->rank++;
            }
        }
    }//Calculation of seats

    print(stu, PEOPLE);
    printf("                    ");
    //fprintf(fp, "                         ");
    for (int i = 0; i < SUB; i++) {
        printf("%-7d", ban_total[i]);
        //fprintf(fp, "%-7d", ban_total[i]);
    }// Total score by subject
    printf("\n");
    //fprintf(fp, "\n");
    printf("                    ");
    //fprintf(fp, "                         ");
    for (int i = 0; i < SUB; i++) {
        printf("%-7.1lf", ban_avg[i]);
        //fprintf(fp, "%-7.1lf", ban_avg[i]);
    }// Average by subject
    printf("\n");
    //fprintf(fp, "\n");
    //line(fp);

    //fclose(fp);
}

The following code was executed: By the way, Only garbage values come out. What's the problem and how do I fix it? I'd appreciate it if you let me know.

c file-io

2022-09-21 18:04

1 Answers

When you open the file, you did it in w+ mode, but w, w+ mode creates a new file and overwrites it even if the file exists. Therefore, the data was not read properly because it was read by fscanf from an empty file.

When reading the data of an existing file, open it in r mode, and if you need to modify it, open it in r+ mode.

Make sure the rest of the code is correct and change it to r mode.

For your information, (stu + i)->id and stu[i].id are equivalent, so both are correct codes.


2022-09-21 18:04

If you have any answers or tips

Popular Tags
python x 4647
android x 1593
java x 1494
javascript x 1427
c x 927
c++ x 878
ruby-on-rails x 696
php x 692
python3 x 685
html x 656

© 2024 OneMinuteCode. All rights reserved.