从文件中的列读取值

时间:2015-10-28 07:01:00

标签: c

我正在尝试从特定列的文件中读取值(忽略第一个数字),然后查找该列的平均值。到目前为止,我有这个代码,用于读取和查找行的平均值,但不适用于列。

#include <stdio.h>
#include <stdlib.h>

int main(){

    FILE *fp;
    int id;
    float mark1;
    float mark2;
    float mark3;
    float mark4;
    int number;
    float average;

    fp= fopen("grades.txt", "r");

    if(fp == NULL){
        printf("File cannot be opened");
        exit(-1);
    }

    while(fscanf(fp, "%d %f %f %f %f", &number, &mark1, &mark2, &mark3, &mark4 ) != EOF){
        //code to calculate average and print output
        //Below is the code I used to find the average of the line (not what I want to do)
        average = (mark1 + mark2 + mark3+ mark4)/4;
        printf("Average for %d : %.2f\n", number, average);

    }

    fclose(fp);
}

所以在文件中总共有5列,第一列是ID,它只是和整数值(int number),其他数字都是浮点数。

文件内容:

12345 60 30 63.2 95
54321 54.2 49 40.5 80
15243 99.5 100 90 98 

3 个答案:

答案 0 :(得分:3)

您可以保留变量sum以添加特定列的所有值,然后使用它来获取平均值,例如

int sum=0, n=0;

while(fscanf(fp, "%d %f %f %f %f", &number, &mark1, &mark2, &mark3, &mark4 ) != EOF){
    //code to calculate average and print output
    //Below is the code I used to find the average of the line (not what I want to do)
    average = (mark1 + mark2 + mark3+ mark4)/4;
    printf("Average for %d : %.2f\n", number, average);

    sum += mark2 //this can be any mark depending on your requirement
    n++;         //this is to keep track of the number of lines

}

printf("the average of column 2 is %d", (sum/n) );

答案 1 :(得分:2)

#include <stdio.h>
#include <stdlib.h>

int main(){

    FILE *fp;
    int id;
    float mark1;
    float mark2;
    float mark3;
    float mark4;
    int number;
    float somme[4];
    int nbLg=0;
    int i;
    fp= fopen("grades.txt", "r");

    if(fp == NULL){
        printf("File cannot be opened");
        exit(-1);
    }

    while(fscanf(fp, "%d %f %f %f %f", &number, &mark1, &mark2, &mark3, &mark4 ) != EOF){
        nbLg++;
        somme[0]+= mark1;
        somme[1]+= mark2;
        somme[2]+= mark3;
        somme[3]+= mark4;

    }

    for(i=0;i<=3;i++) {
        printf("Average for %d : %.2f\n", i+1, somme[i] / (float) nbLg);
    }
    fclose(fp);
}

答案 2 :(得分:0)

以下代码是:

  • 比保持简单,易于理解的必要更复杂。
  • 干净利落地编译
  • 执行所需的功能

一些思考和论文//铅笔工作会给你类似的逻辑

#include <stdio.h>
#include <stdlib.h>

int main( void )
{

    FILE *fp;

    float mark1;
    float mark1Average = 0.0f;
    float mark2;
    float mark2Average = 0.0f;
    float mark3;
    float mark3Average = 0.0f;
    float mark4;
    float mark4Average = 0.0f;
    int   number;
    int   numberAverage = 0;


    fp= fopen("grades.txt", "r");

    if(fp == NULL){
        printf("File cannot be opened");
        exit(-1);
    }

    // get first line to initialize average values
    if  ( 5 != fscanf( fp, "%d %f %f %f %f",
                &numberAverage,
                &mark1Average,
                &mark2Average,
                &mark3Average,
                &mark4Average )
        )
    { // then fscanf failed
        perror( "fscanf for first line of grades.txt failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, fscanf successful

    while(5 == fscanf(fp, "%d %f %f %f %f", &number, &mark1, &mark2, &mark3, &mark4 ))
    {
        numberAverage = (numberAverage+number)/2;
        mark1Average  = (mark1Average+mark1)/2.0f;
        mark2Average  = (mark2Average+mark2)/2.0f;
        mark3Average  = (mark3Average+mark3)/2.0f;
        mark4Average  = (mark4Average+mark4)/2.0f;
    }

    printf( "numberAverage: %d\n", numberAverage);
    printf( "mark1Average:  %f\n", mark1Average);
    printf( "mark2Average:  %f\n", mark2Average);
    printf( "mark3Average:  %f\n", mark3Average);
    printf( "mark4Average:  %f\n", mark4Average);

    fclose(fp);
}