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

问题描述 投票:2回答:1

这个问题在这里已有答案:

我试图使用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);

任何帮助表示赞赏。

c file fopen clion c11
1个回答
3
投票

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

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

fscanf(trafficFile, "%d %d", &temp, &temp2);
© www.soinside.com 2019 - 2024. All rights reserved.