【发布时间】:2020-11-05 03:00:44
【问题描述】:
我是 C 新手,需要一些帮助,当我执行此代码时,输出显示“浮点异常(核心转储)”而不是一个我不知道它可能是什么的数字。我真的不知道我的代码有什么问题,因为我是 Linux 和 C 的初学者。感谢您的每一个帮助。
这是我的functions.c:
#include "header.h"
int count(FILE *file){
int count=0,n;
char word[WORDS];
while(fscanf(file,"%s",word)==1 ){
count++;
};
return count;
};
int total(FILE *file){
int numbers, sum=0;
int token;
while(token!=EOF){
token=fscanf(file,"%d",&numbers);
sum+=numbers;
};
return sum;
};
这是我的 main.c:
#include "header.h"
int main(){
char word[WORDS];
char theFile[WORDS];
FILE *fileptr;
printf("Enter the file name: ");
scanf("%s",theFile);
printf("Reading file %s...\n", theFile);
fileptr=fopen(theFile,"r");
if(fileptr==NULL){
fprintf(stderr,"ERROR: The file '%s' does not exist\n", theFile);
return 1;
};
int theSum=total(fileptr);
int theCount=count(fileptr);
int theMean= theSum/theCount;
printf("Average weight: %d \n", theMean);
return 0;
};
【问题讨论】: