C fscanf 段错误错误 "No source available for "fancyfile() at 0x7fff855e6d39"

标签 c pointers segmentation-fault scanf

我目前正在用 C 语言解决 USACO 的一个旧编码问题。这是我的代码的前几行,其中我尝试使用 fscanf() 函数来获取第一个值,一个 int,来自 blocks.in 文件:

#include <stdio.h>
#include <stdlib.h>
int main() {
     FILE *fin  = fopen ("blocks.in", "r");
     FILE *fout = fopen ("blocks.out", "w");
     int i,j;
     int linecount = 0;
     int alphabetCount[26];
     fscanf(fin," %d",&linecount);

运行 gdb(作为 Eclipse C/C++ IDE 的一部分),我始终收到段错误错误:

fscanf(fin," %d",&linecount);

错误始终显示:

No source available for "flockfile() at 0x7fff855e6d39"

我无法找到问题的根源。我过去没有遇到过任何问题。您是否发现出了什么问题,或者有更好的解决方案/功能来提取数据?

最佳答案

我怀疑您运行程序的目录中没有blocks.in 文件。即使文件存在,也可能无法成功打开。一些简单的错误检查可以帮助您避免出现问题:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    FILE *fin;
    FILE *fout;
    int i,j;
    int linecount = 0;
    int alphabetCount[26];

    if ((fin = fopen("blocks.in", "r")) == NULL) {
        fprintf(stderr, "Unable to open input file\n");
        exit(EXIT_FAILURE);
    }
    if ((fout = fopen("blocks.out", "w")) == NULL) {
        fprintf(stderr, "Unable to open output file\n");
        exit(EXIT_FAILURE);
    }     

    fscanf(fin," %d",&linecount);

    return 0;
}

关于C fscanf 段错误错误 "No source available for "fancyfile() at 0x7fff855e6d39",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41646712/

相关文章:

c++ - Quicksort 出现段错误

c - 对齐堆栈是什么意思?

c - 这个职位增量是如何运作的?

C桶字符串

c - 指向结构体的指针

c - 使用带有指针的比较运算符来检查它是否在地址范围内?

c++ - 尝试指向自动对象时应该使用什么策略?

c++ - 使用复制构造函数 C++ 时出现段错误

c - 指向指针的指针与指针

c - 第一行代码之前的段错误