c - 使用函数从文件中读取动态数组

标签 c

我有以下代码:`

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

void load_from_file(int A[], int *n);

int main()
{
    int *A;
    A = (int*)malloc(0);
    int count = 0;
    int i;

    load_from_file(A, &count);

    for(i = 0; i < count; i++)
    {
        printf("A[%d]=%d ", i, A[i]);
        printf("\n");
        printf("&A[%d]=%p \n\n", i, &A[i]);
    }

    return 0;
}

void load_from_file(int A[], int *n)
{
    FILE* fp;
    int temp, i;

    fp = fopen("data.txt", "r");
    if (fp == NULL)
    {
        printf("error!!");
        exit (1);
    }

    fscanf(fp, "%d", &temp);
    *n = temp;

    A = (int*) realloc(A, temp * sizeof(int));
    if (*A == NULL)
    {
        printf("error realloc!!");
        exit(1);
    }

    for(i = 0; i < temp; i++)
    {
        fscanf(fp, "%d", &A[i]);
    }

    for(i = 0; i < temp; i++)
    {
        printf("A[%d]=%d ", i, A[i]);
        printf("\n");
        printf("&A[%d]=%p \n\n", i, &A[i]);
    }

    fclose(fp);
}

我正在尝试将文本文件读入数组。 文件的第一行是数组元素的数量,第二行是数字元素。 我们通过 realloc 创建数组。 但是出了点问题...... 我有一些补丁,打印数组元素的地址。

但不幸的是它们在函数内部和函数外部是不同的(并非所有时候),尽管数组是通过引用传递的(我认为...)

请告诉我错误在哪里,我该如何解决这个问题。

提前致谢...

迪米特里

最佳答案

调用:

int *A = load_from_file(&count);

功能:

int *load_from_file(int *n)
{
    FILE* fp;
    int temp,i;
    int *A = 0;

    fp=fopen("data.txt","r");
    if (fp==NULL){fprintf(stderr, "error!!\n");exit (1);}

    fscanf(fp,"%d",&temp);
    *n=temp;
    A = (int*) realloc(A,temp * sizeof(int));
    if (A == NULL) {fprintf(stderr, "error realloc!!\n");exit(1);}
    for(i=0;i<temp;i++)
        fscanf(fp, "%d",&A[i]);
    for(i=0;i<temp;i++)
    {
        printf("A[%d]=%d ",i,A[i]);
        printf("\n");
        printf("&A[%d]=%p \n\n",i,&A[i]);
    }
    fclose(fp);
    return A;
}

这是一组完全最小的更改;代码需要做更多的工作。特别是,应该对 fscanf() 调用进行错误检查。分配测试中有一个微妙但重要的变化:if (A == NULL) 而不是原来的 if (*A == NULL)


更完整的错误检查:

int *load_from_file(int *n)
{
    FILE *fp;
    int temp, i;
    int *A = 0;
    const char file[] = "data.txt";

    fp = fopen(file, "r");
    if (fp == NULL)
    {
        fprintf(stderr, "Failed to open file %s for reading\n", file);
        exit(1);
    }

    if (fscanf(fp, "%d", &temp) != 1)
    {
        fprintf(stderr, "Failed to read number of entries\n");
        exit(1);
    }
    *n = temp;
    A = (int *) realloc(A, temp * sizeof(int));
    if (A == NULL)
    {
        fprintf(stderr, "Failed to reallocate %zu bytes of memory\n",
                temp * sizeof(int));
        exit(1);
    }
    for (i = 0; i < temp; i++)
    {
        if (fscanf(fp, "%d", &A[i]) != 1)
        {
            fprintf(stderr, "Failed to read entry number %d\n", i);
            exit(1);
        }
    }
    for (i = 0; i < temp; i++)
        printf("A[%d]=%d: &A[%d]=%p\n", i, A[i], i, &A[i]);
    fclose(fp);
    return A;
}

我有一个错误报告函数库,可以将每个错误的错误处理从 4 行减少到 1 行。我强烈建议您自己创建和使用这样的库。

关于c - 使用函数从文件中读取动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22489902/

相关文章:

c - shellcode执行/bin/sh但不执行./abcde

C - 读取一串数字

c - 如何判断输入值是否为数字?

使用C计算目录中的文件数

c - 如何在C语言的数组结构中使用qsort()

c - 超出范围是否释放了(字符)数组的内存?

c - 在 C 中按下 Ctrl-L 时打印一些内容

c - 在 C 预处理器宏中是否有相当于 Haskell 的 `let`/`in`?

我可以使用标记粘贴来处理编译时命名吗?

c - 如果使用 valgrind 启动,信号处理程序中的 snprintf 会创建段错误