C 动态内存分配 - 从文件读取数据

标签 c dynamic-memory-allocation fgets calloc

我正在努力从 MATLAB 复制 load() 函数以在 C 应用程序中使用。我在动态加载数据和初始化我需要的数组时遇到问题。更具体地说,我尝试将 fgets 与已使用 calloc 初始化的数组一起使用,但无法使其工作。该功能如下,感谢帮助。

编辑:更新的代码位于以下有缺陷的示例下方。

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

void *load(const char *Filename);

void *load(const char *Filename)
{
    FILE* FID;
    if ((FID = fopen(Filename, "r")) == NULL)
    {
        printf("File Unavailable.\n");
    }
    else
    {
        int widthCount = 0, heightCount = 0;

        char ReadVal;
        while ((ReadVal = fgetc(FID)) != '\n')
        {
            if (ReadVal == ' ' || ReadVal == ',' || ReadVal == '\t')
            {
                widthCount++;
            }
        }

        rewind(FID);
        char* String = calloc(widthCount * 100, sizeof(char));
        while (fgets(*String, widthCount+1, FID) != EOF)
        {
            heightCount++;
        }
        double* Array = calloc(widthCount * heightCount, sizeof(double));
        rewind(FID);
        int i = 0, j = 0;
        char * pch;
        while (fgets(*String, widthCount+1, FID) != EOF)
        {
            pch = strtok(String, " ,\t");
            while (pch != NULL)
            {
                Array[i][j] = strtod(pch, NULL);
                pch = strtok (NULL, " ,\t");
                j++;
            }
            i++;
            j = 0;
        }

        fclose(FID);
        return Array;

    }

}

修改后的代码: 对于任何遇到类似问题的人来说,这个解决方案都是有效的。

void *load(const char *Filename)
{
    FILE* FID;
    if ((FID = fopen(Filename, "r")) == NULL)
    {
        printf("File Unavailable.\n");
        return NULL;
    }
    else
    {   
        int widthCount = 0, heightCount = 0;
        double *Array;
        char Temp[100];
        while ((Temp[0] = fgetc(FID)) != '\n')
        {
            if (Temp[0] == '\t' || Temp[0] == ' ' || Temp[0] == ',')
            {
                widthCount++;
            }
        }
        widthCount++;
        //printf("There are %i columns\n", widthCount);
        rewind(FID);
        while (fgets(Temp, 99, FID) != NULL)
        {
            heightCount++;
        }
        //printf("There are %i rows\n", heightCount);
        Array = (double *)calloc((widthCount * heightCount), sizeof(double));
        rewind(FID);
        int i = 0;
        while (!feof(FID))
        {

            fscanf(FID, "%lf", &*(Array + i));
            fgetc(FID);
            i++;
        }

        return Array;   
    }
}

最佳答案

Array 不是二维数组,而是 Array[i][j] = strtod(pch, NULL); 只是增加指针 *(Array++) = strtod(pch, NULL);

关于C 动态内存分配 - 从文件读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10015585/

相关文章:

c - 警告 : control reaches end of non-void function; error: null undeclared (c)

评论长度超过一行

c - 使用 fgets 读取表达式并使用 sscanf 获取数值

c - 重用 malloc() 分配的内存空间

c - 使用 malloc 的 MPI 动态数组

c - 微型计算机,串口。请求多个字节

c - 学习动态内存分配

c - Valgrind:大小 1 的读/写无效

c - 为什么我会收到此错误以及如何修复它 : munmap_chunk(): invalid pointer: 0x0000000000400694 *** Hello WorldAborted

c - 一种仅在第一行进行 fscanf 的方法