为 Linux 创建 C 程序时出现浮点异常(核心转储)

Posted

技术标签:

【中文标题】为 Linux 创建 C 程序时出现浮点异常(核心转储)【英文标题】:Floating point exception (core dumped) while creating a C program for Linux 【发布时间】: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;
;

【问题讨论】:

【参考方案1】:

获得浮点异常的主要原因是您正在访问超出其大小的文件。

在函数 totalcount 中,您使用的是相同的指针,因此当使用 total 完成时,文件指针位于文件末尾,您也在 count 中使用相同的指针。

你需要做fseek(file,SEEK_SET,0);,使指针指向开头。

而且你们所有的块语句和函数都以;结尾,这是错误的。

我已经更正了整个程序,假设文件的内容只是像这样的数字1 2 3 4 5 6

#include <stdio.h>

#define WORDS 100

int count(FILE *file)
    int count = 0,n; 
    char word[WORDS];
    // you need this to access the elements from start of the file
    // comment below line causes sigfpe, Floating point exception
    fseek(file,SEEK_SET,0);
    printf(" position of fptr in count = %d\n",ftell(file));
    while(fscanf(file,"%s",word)==1 ) 
        count++;
    
    return count;


int total( FILE *file ) 
    int numbers, sum = 0;
    int token;
    // This is checked after number is read, will add the last number 2 times 
    //while(token != EOF) 
    while(1)
    
        token = fscanf(file,"%d",&numbers);
        printf("N = %d, token = %d\n", numbers, token);
        if(token == EOF )
            break;
        sum += numbers;
    
    printf(" position of fptr in total  at the end = %d, sum = %d\n",ftell(file), sum);
    return sum;


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); 
    //make sure to add a check that `theCount` is not zero
    int theMean = theSum/theCount; 

    printf("Average weight: %d \n", theMean);

    return 0;

【讨论】:

【参考方案2】:

在此声明中

int theMean= theSum/theCount;

theCount 为 0 时,您将除以零,这是未定义的行为,可能会导致 FPE。

【讨论】:

以上是关于为 Linux 创建 C 程序时出现浮点异常(核心转储)的主要内容,如果未能解决你的问题,请参考以下文章

当我在 Linux 上运行使用硬浮点选项构建的 Neon 代码时出现分段错误

为依赖于其他项目的 Spring Boot 应用程序创建胖 jar 时出现异常

为啥在编译我的代码C(linux)时出现分段错误(核心转储)[关闭]

使用 C# 和 XAML 为 Windows 8.1 创建 facebook 应用程序时出现异常

参数/列未绑定将C#应用程序连接到SAP HANA数据库时出现异常

Vigenere.c CS50 浮点异常(核心转储)