Jor*_*ndo 2 c pointers scanf segmentation-fault
我正在处理来自USACO的旧编码问题.这是我的代码的前几行,其中我试图使用该fscanf()函数int从blocks.in文件中获取第一个值a :
#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);
Run Code Online (Sandbox Code Playgroud)
运行gdb(作为Eclipse C/C++ IDE的一部分),我一直在线上遇到分段错误错误:
fscanf(fin," %d",&linecount);
Run Code Online (Sandbox Code Playgroud)
该错误始终如下:
没有可用于"flockfile()的源代码,位于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;
}
Run Code Online (Sandbox Code Playgroud)