使用fscanf()读取文本文件时出错

时间:2018-03-05 02:28:18

标签: c file fopen clion c11

我正在尝试使用fscanf()从文本文件中读取并获得输出:

Process finished with exit code -1073741819 (0xC0000005)

第一次调用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);

感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

您需要将整数变量的地址传递给fscanf()

fscanf(trafficFile, "%d", &n);

fscanf(trafficFile, "%d %d", &temp, &temp2);