xcode:线程1:exc_bad_access(代码= 1,地址= 0x68)fscanf

发布于 2025-01-21 07:12:57 字数 813 浏览 1 评论 0原文

我正在使用以下代码,并且正在接收线程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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如何视而不见 2025-01-28 07:12:57

将注释转换为答案。

我的直接猜测是您未能打开文件 - 您不检查的错误,必须始终检查。文件为AWOL,否则该程序是从错误的目录运行,或者它们的权限错误。

if (file == NULL)
{
    fprintf(stderr, "Failed to open file '%' for reading\n", "somefile.txt");
    exit(EXIT_FAILURE);
}

文件名的重复文字显示了为什么您切勿将字符串字面的字符串作为fopen();您应该有一个变量,以便您也可以在错误消息中报告文件名,而无需重复自己。

const char *filename = "somefile.txt";
if ((file = fopen(filename, "r")) == NULL)
{
    fprintf(stderr, "Failed to open file '%' for reading\n", filename); n. 
    exit(EXIT_FAILURE);
}

实际上,您可能应该将文件的名称传递给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()要返回加载多少个数字,以免访问卸载的数字。

将所有这些更改放在一起可能会产生:

#include <stdio.h>
#include <stdlib.h>

#define MAX_NUMBER 500001

static size_t load_file(const char *filename, size_t arrsiz, int *array)
{
    FILE *file;
    if ((file = fopen(filename, "r")) == NULL)
    {
        fprintf(stderr, "Failed to open file '%s' for reading\n", filename);
        exit(EXIT_FAILURE);
    }

    size_t i = 0;
    int num;
    while (i < arrsiz && fscanf(file, "%d", &num) > 0)
        array[i++] = num;

    fclose(file);

    return i;
}

int main(void)
{
    int a[MAX_NUMBER];
    size_t num = load_file("somefile.txt", MAX_NUMBER, a);

    for (size_t i = 0; i < num; i++)
        printf("%d\n", a[i]);

    return 0;
}

已编译但不运行。

您可以处理这样的命令行:

int main(int argc, char **argv)
{
    if (argc > 2)
    {
        fprintf(stderr, "Usage: %s [filename]\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    const char *filename = (argc == 2) ? argv[1] : "somefile.txt";

    int a[MAX_NUMBER];
    size_t num = load_file(filename, MAX_NUMBER, a);

    for (size_t i = 0; i < num; i++)
        printf("%d\n", a[i]);

    return 0;
}

或者可以允许多个参数并迭代所有参数。
有时,最好在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.

if (file == NULL)
{
    fprintf(stderr, "Failed to open file '%' for reading\n", "somefile.txt");
    exit(EXIT_FAILURE);
}

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.

const char *filename = "somefile.txt";
if ((file = fopen(filename, "r")) == NULL)
{
    fprintf(stderr, "Failed to open file '%' for reading\n", filename); n. 
    exit(EXIT_FAILURE);
}

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 use strerror(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. Your main() also assumes that the array was filled. Revise load_file() to return how many numbers were loaded so you don't access unloaded numbers.

Putting all those changes together might yield:

#include <stdio.h>
#include <stdlib.h>

#define MAX_NUMBER 500001

static size_t load_file(const char *filename, size_t arrsiz, int *array)
{
    FILE *file;
    if ((file = fopen(filename, "r")) == NULL)
    {
        fprintf(stderr, "Failed to open file '%s' for reading\n", filename);
        exit(EXIT_FAILURE);
    }

    size_t i = 0;
    int num;
    while (i < arrsiz && fscanf(file, "%d", &num) > 0)
        array[i++] = num;

    fclose(file);

    return i;
}

int main(void)
{
    int a[MAX_NUMBER];
    size_t num = load_file("somefile.txt", MAX_NUMBER, a);

    for (size_t i = 0; i < num; i++)
        printf("%d\n", a[i]);

    return 0;
}

That's been compiled but not run.

You could process a command-line argument like this:

int main(int argc, char **argv)
{
    if (argc > 2)
    {
        fprintf(stderr, "Usage: %s [filename]\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    const char *filename = (argc == 2) ? argv[1] : "somefile.txt";

    int a[MAX_NUMBER];
    size_t num = load_file(filename, MAX_NUMBER, a);

    for (size_t i = 0; i < num; i++)
        printf("%d\n", a[i]);

    return 0;
}

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 from stdin if there are no command-line arguments. The options are legion!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文