Using structure in reading a file, calculating, and writing into another

1.2k views Asked by At

I have an input data file called "TEST.txt". it contains id numbers, names, grades of three different exams of ten students. I'm trying to make a program that reads these input data, calculates the mean value of exams for each student, and writes again id numbers,names, averages, of students whose averages are >=45.5 into output file called "RESULT.TXT" using structures. I think I am able to read my input data with the structure I defined. I want to know what can I do for finding the averages of exams (one,two, and three), setting the condition of writing average values, and writing ids,names, and averages into RESULTS.TXT. Here is my code until now.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
 typedef struct student{
    char name[30];
    int id;
    int exam1;
    int exam2;
    int exam3;




}STUDENT;



int main(){
    int sum=0;
    int i=0;
    double mean[10];


    STUDENT test[10]; 
    FILE *fRead;


    fRead=fopen("TEST.txt","r+");
    if((fRead=fopen("TEST.txt","r"))==NULL){
        printf("File could not be opened\n");       
    }

    while(!feof(fRead)){


            fscanf(fRead,"%d%s%d%d%d",&(test[i].id),test[i].name,&(test[i].exam1),&(test[i].exam2),&(test[i].exam3));


            printf("\n%s\n",test[i].name);

            i++;    
    }

    return 0;
}
2

There are 2 answers

2
user3629249 On BEST ANSWER

the following code:

  1. is one possible way to perform the desired function:
  2. cleanly compiles
  3. uses fscanf() properly
  4. does not use undesireable functions like feof()
  5. is appropriately commented
  6. performs appropriate error checking
  7. does not include unnecessary header files
  8. will handle any number of students in the input file, including 0 students

and now the code

#include <stdio.h>   // fopen(), fscanf(), fclose()
#include <stdlib.h>  // exit(), EXIT_FAILURE

#define MAX_NAME_LEN (29)

struct student
{
    char name[MAX_NAME_LEN+1]; // +1 to allow for NUL terminator byte
    int id;
    int exam1;
    int exam2;
    int exam3;
};

int main( void )
{
    struct student test;

    FILE *fRead  = NULL;
    FILE *fWrite = NULL;

    if((fRead=fopen("TEST.txt","r"))==NULL)
    {
        perror("fopen for read of Test.txt failed");
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    if( (fWrite = fopen( "RESULT.TXT", "w" )) == NULL )
    {
        perror( "fopen for write of RESULT.TXT failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    while( 5 == fscanf(fRead,"%d %" MAX_NAME_LEN "s %d %d %d",
           &(test.id),
           test.name,
           &(test.exam1),
           &(test.exam2),
           &(test.exam3)) )
    {
        printf("\n%s\n",test.name);

        float sum  = (float)test.exam1 + test.exam2 + test.exam3;
        float mean = sum / 3.0f;

        if( 45.5f < mean )
        {
            fprintf( fWrite, "%d %s %d %d %d %2.2f\n",
              test.id,
              test.name,
              test.exam1,
              test.exam2,
              test.exam3,
              mean );
        } // end if
    } // end while

    fclose( fRead );
    fclose( fWrite );

    //return 0; == not needed when returning from 'main' and value is zero
} // end function: main
6
zeek-r On

I have made a few changes to the code you have written, 1. I have used loop instead of array of structure, the constant defines the number of students to scan.

  1. I have created an fOut pointer for RESULT.txt, which I opened in append mode.

  2. I created a condition, where if mean > 45.5, the details of the students will be printed in RESULT.txt.

    P.S. I checked the program with 1 input in test file. Test it with multiple line inputs, should work fine.

    #include <stdio.h>
    #include <stdlib.h>
    
    //constant for number of students, can be changed according to requirement
    #define numOfStudents 10
    
    typedef struct student{
    char name[30];
    int id;
    int exam1;
    int exam2;
    int exam3;
    }STUDENT;
    
    int main(){
        double sum=0;
        int i=0;
        double mean;
    
        STUDENT test; 
        FILE *fRead;
    
        //File pointer for output
        FILE * fOut;
    
        //File  for output in append mode
        fOut = fopen("RESULT.txt", "a+");       
    
    
        fRead=fopen("TEST.txt","r+");
    
        if(fRead == NULL)
        {
            printf("File could not be opened\n");       
        }
    
       //check if the file is successfully opened for appending
        if(fOut == NULL)
        {
            printf("File could not be opened for printing\n"); 
        }
    
    
        for(i; i < numOfStudents; i++)
        {  
             fscanf(fRead,"%d%s%d%d%d",&(test.id),test.name,&(test.exam1), &(test.exam2), &(test.exam3));
             //calculates mean
             sum = test.exam1 + test.exam2 + test.exam3;
             mean = sum / 3.0;
    
             //Condition for mean to be printed to output file
             if(mean > 45.5)               
             {
                fprintf(fOut, "%d %s %d %d %d", (test.id),test.name, (test.exam1),(test.exam2),(test.exam3 ));
                fprintf(fOut, "\n");
             }
    
             if(feof(fRead))
             {
                break;
             }
         }
         fclose(fRead);
         fclose(fOut);
         return 0;
    }