我是 C 编程的新手,我得到一个线程 1:EXC_BAD_ACCESS(代码 = 1,地址 0x68) 当我运行我的程序时。我的代码的目的是从包含正数和负数的 txt 文件中读取数据并对其进行处理。
#include <stdio.h>
int main (int argc, const char * argv[]) {
FILE *file = fopen("data.txt", "r");
int array[100];
int i = 0;
int num;
while( fscanf(file, "%d" , &num) == 1) { // I RECEIVE THE ERROR HERE
array[i] = num;
printf("%d", array[i]);
i++;
}
fclose(file);
for(int j = 0; j < sizeof(array); j++){
printf("%d", array[j]);
}
}
最佳答案
之后
FILE *file = fopen("data.txt", "r");
说
if(file == 0) {
perror("fopen");
exit(1);
}
只是猜测,其余代码看起来没问题,所以很可能就是这个问题。
关于c - 尝试从 C 中的 txt 文件中读取数字时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19148740/