使用结构读取文件,计算并写入另一个文件

时间:2016-12-25 15:37:18

标签: c arrays structure file-writing file-processing

我有一个名为" TEST.txt"的输入数据文件。它包含十个学生的三个不同考试的身份证号码,姓名,成绩。我试图创建一个程序来读取这些输入数据,计算每个学生的考试平均值,并再次将平均值> = 45.5的学生的ID号,姓名,平均值写入名为&的输出文件中#34;的Result.txt"使用结构。 我想我能够用我定义的结构读取输入数据。我想知道如何找到考试的平均值(一,二和三),设置写平均值的条件,并将ID,名称和平均值写入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;
}

2 个答案:

答案 0 :(得分:0)

我对您编写的代码进行了一些更改,  1.我使用了循环而不是结构数组,常量定义了要扫描的学生数量。

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

  2. 我创建了一个条件,如果是>&gt; 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 :(得分:0)

以下代码:

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

    #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
    
相关问题