尝试从C中的txt文件读取数字时出错 [英] Error when trying to read in numbers from txt file in C
本文介绍了尝试从C中的txt文件读取数字时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是C语言编程的新手,我得到了一个线程1:EXC_BAD_ACCESS(代码= 1,地址0x68)当我运行程序时.我的代码的目的是从包含正数和负数的txt文件中读取内容,并对其进行处理.
I am new to C programming and I am getting a THREAD 1: EXC_BAD_ACCESS(code = 1, address 0x68) when I run my program. The purpose of my code is to read from a txt file that contains positive and negative numbers and do something with it.
#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);
}
仅凭猜测,其余代码看起来还不错,所以很可能是问题所在.
Just a guess, the rest of the code looks ok, so likely this is the problem.
这篇关于尝试从C中的txt文件读取数字时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文