xcode:线程1:exc_bad_access(代码= 1,地址= 0x68)fscanf
我正在使用以下代码,并且正在接收线程1:exc_bad_access(代码= 1,地址= 0x68)
错误。我可以更好地执行此操作?我只是在加载一个大约500000号的TXT文件,它们每个都在新线路上。我研究了一些资源如何做到这一点,但最终以这些奇怪的是。我希望AC Guru能帮助我。
#include <stdio.h>
#include <time.h>
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define COUNT_ARRAY_LENGTH 10
#define MAX_NUMBER 500001
int *load_file(){
FILE *file;
file = fopen("somefile.txt", "r");
int a[MAX_NUMBER];
int i=0;
int num;
while(fscanf(file, "%d", &num) > 0) {
a[i] = num;
i++;
}
fclose(file);
return a;
}
int main(int argc, const char *argv[])
{
int *a;
a = load_file();
for(int i = 0; i < MAX_NUMBER; i++){
printf("%d\n", a[i]);
}
return 0;
}
I'm using the following code below and I'm receiving a Thread 1: EXC_BAD_ACCESS (code=1, address=0x68)
error. What are some ways I can execute this better? I'm simply loading a txt file that has roughly 500000 numbers and they are each on a new line. I've looked at a few resources how to do this, but I end up with these oddities. I'm hoping a c guru can help me out.
#include <stdio.h>
#include <time.h>
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define COUNT_ARRAY_LENGTH 10
#define MAX_NUMBER 500001
int *load_file(){
FILE *file;
file = fopen("somefile.txt", "r");
int a[MAX_NUMBER];
int i=0;
int num;
while(fscanf(file, "%d", &num) > 0) {
a[i] = num;
i++;
}
fclose(file);
return a;
}
int main(int argc, const char *argv[])
{
int *a;
a = load_file();
for(int i = 0; i < MAX_NUMBER; i++){
printf("%d\n", a[i]);
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将注释转换为答案。
我的直接猜测是您未能打开文件 - 您不检查的错误,必须始终检查。文件为AWOL,否则该程序是从错误的目录运行,或者它们的权限错误。
文件名的重复文字显示了为什么您切勿将字符串字面的字符串作为
fopen()
;您应该有一个变量,以便您也可以在错误消息中报告文件名,而无需重复自己。实际上,您可能应该将文件的名称传递给
load_file()
函数,以便您可以更轻松地更改它(例如,通过命令行参数)。那也是传递数组和数组的大小。这为您提供了更通用的功能,一个更容易适应其他用途。您还可以
#include&lt; errno.h&gt;
and#include&lt; string.h&gt;
并使用strerror(errno)
以打印系统错误消息为用户提供更多帮助(但是知道文件名是朝正确方向迈出的重要一步)。另外,您应该拥有
,而(i&lt; max_number&amp;&amp;&amp; fscanf(file,“%d”,&amp; num)&gt; 0)
,因此您不会溢出数组。另外,您要返回load_file()中的本地数组的地址 - 您不能安全地做到这一点。在
main()
中定义数组,然后将指针作为参数传递。您的main()
还假定阵列已填充。修订load_file()
要返回加载多少个数字,以免访问卸载的数字。将所有这些更改放在一起可能会产生:
已编译但不运行。
您可以处理这样的命令行:
或者可以允许多个参数并迭代所有参数。
有时,最好在
main()
中进行文件打开和关闭,然后将打开的文件流传递到该功能。然后,如果没有命令行参数,则可以从stdin
阅读。选项是军团!Converting comments into an answer.
My immediate guess would be that you're failing to open the file — an error you don't check for and must always check for. Files go AWOL, or the program is run from the wrong directory, or they have the wrong permissions.
The repeated literal for the file name shows why you should never pass a string literal as the file name to
fopen()
; you should have a variable so that you can report the file name in the error message too, without repeating yourself.In fact, you should probably pass the name of the file to be loaded to the
load_file()
function so that you can more easily change it (by command line arguments, for example). That is as well as passing the array, and the size of the array. That gives you a more generic function, one more easily adapted to other uses.You could also
#include <errno.h>
and#include <string.h>
and usestrerror(errno)
to print the system error message to give more help to the user (but knowing the file name is a huge step in the right direction).Also, you should have
while (i < MAX_NUMBER && fscanf(file, "%d", &num) > 0)
so you don't overflow the array.Also, you're returning the address of the local array in load_file() — you can't do that safely. Define the array in
main()
and pass the pointer as a parameter. Yourmain()
also assumes that the array was filled. Reviseload_file()
to return how many numbers were loaded so you don't access unloaded numbers.Putting all those changes together might yield:
That's been compiled but not run.
You could process a command-line argument like this:
Or you could allow more than one argument and iterate over them all.
Sometimes, it's better to do the file opening and closing in the
main()
and pass the open file stream to the function. You can then read fromstdin
if there are no command-line arguments. The options are legion!