gpt4 book ai didi

c - 为什么我收到 "Segmentation fault: 11"

转载 作者:行者123 更新时间:2023-11-30 20:31:59 26 4
gpt4 key购买 nike

当我在终端中运行它时,它只会告诉我这是一个段错误。该程序读入一个名为 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);
}
}

最佳答案

您的代码在c99下编译,以在for之外移动i声明来编译它

我在我的系统上测试了它并且它有效(就逻辑而言)

这意味着特定函数 fopenfscanfscanf 之一失败 - 您必须检查错误值

关于c - 为什么我收到 "Segmentation fault: 11",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49117165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com