简体   繁体   English

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

[英]Floating point exception (core dumped) while creating a C program for Linux

I am new to C and need some help, when I execute this code, the output shows "Floating point exception (core dumped)" instead of a number which I don't know what it could be.我是 C 新手,需要一些帮助,当我执行此代码时,输​​出显示“浮点异常(核心转储)”而不是我不知道它可能是什么的数字。 I don't really know what is wrong with my code since I am a total beginner with Linux and C. Every help is appreciated.我真的不知道我的代码有什么问题,因为我是 Linux 和 C 的完全初学者。感谢每一个帮助。

Here is my functions.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;
};

and here is my main.c:这是我的 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;
};

In this statement在这份声明中

int theMean= theSum/theCount;

when theCount is 0 you are dividing by zero, which is undefined behavior and is probably causing the FPE.theCount为 0 时,您除以零,这是未定义的行为,可能会导致 FPE。

The main reason for getting Floating point exception is that you are accessing the file beyond its size.获得浮点异常的主要原因是您访问的文件超出其大小。

In function total and count you are using the same pointer, so when done with total the file pointer is at the end of file, and you are using the same in count also.在函数totalcount您使用相同的指针,因此当完成total ,文件指针位于文件末尾,并且您在count也使用相同的指针。

you need to do fseek(file,SEEK_SET,0);你需要做fseek(file,SEEK_SET,0); , to make the pointer point at the beginning. , 使指针指向开头。

and all of you block statements and functions are end by ;并且所有块语句和函数都以;结尾; , thats wrong. ,那是错的。

I have corrected the program as a whole assuming that the contents of the file are just numbers like this 1 2 3 4 5 6我已经更正了整个程序,假设文件的内容只是像这样的数字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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM