我为什么收到“Segmentation fault:11”

问题描述 投票:-2回答:1

当我在终端中运行它时,它只会告诉我这是一个分段错误。该程序读入一个名为DATA的文件。这是一个包含考试成绩的txt文件。分数如下(如果完全相关):10 25 50 50 5 0 0 45 45 15 25 50 2 50 30 40。

    #include <stdio.h>
int main()
{
    FILE *fp = fopen("DATA", "r");   //Opens the file DATA in read mode.
    int possibleScoreCounts[51] = {0};
    int score;
    while(!feof(fp))
    {
       fscanf(fp, "%d", &score);
       possibleScoreCounts[score]++;
    }
    printf("Enter a score to check on the exam: ");
    scanf("%d", &score);
    while(score != -1)
    {
       int count = 0;
       for(int i = 0; i < score; i++)
           count = count + possibleScoreCounts[i];
       printf("%d scored lower than %d\n", count, score);
       printf("Enter a score to check on the exam: ");
        scanf("%d", &score);
    }
}
c
1个回答
0
投票

你的代码在c99下编译,用i之外的移动for声明编译它

我在我的系统上测试了这个并且它可以工作(就逻辑而言)

这意味着其中一个特定函数fopenfscanfscanf failes - 您必须检查错误值

© www.soinside.com 2019 - 2024. All rights reserved.