c - 为 Linux 创建 C 程序时出现浮点异常(核心转储)
问题描述
我是 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;
};
解决方案
在这份声明中
int theMean= theSum/theCount;
当theCount
为 0 时,您除以零,这是未定义的行为,可能导致 FPE。
推荐阅读
- c# - ASP.net 表不会在“tbody”中显示项目
- vba - Excel Web Scraper - 受密码保护的网站
- jquery - jQuery ajax 处理 204 No Content
- r - 是否可以在 DT 数据表中添加复选框?
- java - 返回意外结果的 Java 方法
- mysql - 从包含的连接创建临时表+ 6500万条记录Mysql
- mysql - SQL多个更新查询一个接一个
- r - 如何使用对象名称从环境对象创建命名列表或向量?
- ios - 即使设置了高度,UIScrollView 也无法正常工作
- node.js - 如何将 create-react-app 作为大型网站的子目录提供服务?