c语言中输出文件的行数、每一列的总和、每一列的平均值
1、
#include <stdio.h> int main(void) { FILE *fp; int lines = 0; double c1, c2, c3; double c1sum = 0, c2sum = 0, c3sum = 0; if((fp = fopen("a.txt","r")) == NULL) printf("\afile does not exist!\n"); else { while(fscanf(fp, "%lf%lf%lf", &c1, &c2, &c3) == 3) { printf("%8.2f%8.2f%8.2f\n", c1, c2, c3); lines++; c1sum += c1; c2sum += c2; c3sum += c3; } puts("\n======================\n"); printf("the total lines: %d\n", lines); puts("the sum of every column are as below."); printf("%8.2f%8.2f%8.2f\n\n", c1sum, c2sum, c3sum); puts("the average are as below."); printf("%8.2f%8.2f%8.2f\n", c1sum/lines, c2sum/lines, c3sum/lines); fclose(fp); } return 0; }