我目前正在用 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/