在读取文件、计算和写入另一个文件时使用结构

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

我有一个名为 "TEST.txt" 的输入数据文件。它包含十个学生的三个不同考试的 ID 号、姓名、成绩。我正在尝试制作一个程序来读取这些输入数据,计算每个学生的考试平均值,然后再次将平均值 >=45.5 的学生的 ID 号、姓名、平均值写入名为 [=14= 的输出文件中] 使用结构。 我想我可以用我定义的结构读取我的输入数据。我想知道我能做些什么来找到考试的平均值(一,二,三),设置写入平均值的条件,并将ids,names和平均值写入RESULTS.TXT。 到目前为止,这是我的代码。

#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;
}

我对你写的代码做了一些修改, 1. 我用了循环而不是结构数组,常数定义了要扫描的学生数。

  1. 我为 RESULT.txt 创建了一个 fOut 指针,我以追加模式打开它。

  2. 我创建了一个条件,如果 mean > 45.5,学生的详细信息将打印在 RESULT.txt.

    P.S。我在测试文件中检查了 1 个输入的程序。用多行输入测试它,应该可以正常工作。

    #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;
    }
    

以下代码:

  1. 是执行所需功能的一种可能方式:
  2. 干净地编译
  3. 正确使用 fscanf()
  4. 不使用像feof()
  5. 这样的不良函数
  6. 评论得当
  7. 执行适当的错误检查
  8. 不包含不必要的头文件
  9. 将处理输入文件中任意数量的学生,包括 0 名学生

现在是代码

#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