This question already has an answer here:
这个问题在这里已有答案:
- Got stuck with Segmentation Fault Error 2 answers
陷入了分段错误错误2的答案
I am trying to read from a text file using fscanf()
and I am getting the output:
我试图使用fscanf()从文本文件中读取,我得到输出:
Process finished with exit code -1073741819 (0xC0000005)
The crash happens the first time fscanf()
is called. here is my code:
崩溃发生在第一次调用fscanf()时。这是我的代码:
int i=0;
FILE * trafficFile;
trafficFile = fopen("../trafficCount.txt","r");
if (trafficFile == NULL){
printf("Could not open traffic file\n");
}
int n = 30;
fscanf(trafficFile,"%d",n);
printf("%d",n);
for (i = 0; i < n; i++){
int temp = 0;
int temp2 = 0;
fscanf(trafficFile, "%d %d", temp, temp2);
printf("%d %d\n", temp, temp2);
}
fclose(trafficFile);
Any help is appreciated.
任何帮助表示赞赏。
1 个解决方案
#1
3
You need to pass in the address of the integer variables to fscanf()
你需要将整数变量的地址传递给fscanf()
fscanf(trafficFile, "%d", &n);
fscanf(trafficFile, "%d %d", &temp, &temp2);
#1
3
You need to pass in the address of the integer variables to fscanf()
你需要将整数变量的地址传递给fscanf()
fscanf(trafficFile, "%d", &n);
fscanf(trafficFile, "%d %d", &temp, &temp2);