C编程:从文本文件中读取(双倍)数字,逐行读取2D数组

C编程:从文本文件中读取(双倍)数字,逐行读取2D数组,c,multidimensional-array,C,Multidimensional Array,我有一个用excel创建的文本文件,其中包含一个尺寸为36x35的表格。值为双倍数字,例如2.58,文本文件如下所示: 1.25 2.31 ... 4.28 2.56 ... 3.27 ... ... ... 我知道表arr_行、arr_列的维度,因此声明一个动态二维数组: double **MUDG_table; //dynamic allocate array of MUDG_table (1st Dimension) MUDG_table = calloc(arr_row,s

我有一个用excel创建的文本文件,其中包含一个尺寸为36x35的表格。值为双倍数字,例如2.58,文本文件如下所示:

1.25  2.31 ...
4.28  2.56 ...
3.27  ...
...   ...
我知道表arr_行、arr_列的维度,因此声明一个动态二维数组:

double **MUDG_table;

//dynamic allocate array of MUDG_table (1st Dimension)
MUDG_table = calloc(arr_row,sizeof(int *));

//check if the memory has been allocated correctly
if (MUDG_table==NULL) 
{
    printf("Error allocating memory!\n"); //print an error message
    return 1; //return with failure
}

for (cv02=0;cv02<arr_row;cv02++)
{
    //dynamic allocate array of MUDG_table (2nd Dimension)
    MUDG_table[cv02] = calloc(arr_column, sizeof(int));

    //check if the memory has been allocated correctly
    if (MUDG_table[cv02]==NULL) 
    {
        printf("Error allocating memory!\n"); //print an error message
        return 1; //return with failure
    }
}
到目前为止还不错。然后,我尝试从文本文件中读取值,并将其存储到数组中进行进一步处理:

//************************************************************************************************************//
//read the text file with the values of the gain and save it to the MUDG_table
//************************************************************************************************************//

gain_ptr = fopen("MUDG_text.txt", "r");

if (gain_ptr == NULL)
{
    printf("Error Reading File\n");
    return 1; //return with failure
}

for (row=0;row<arr_row;row++)
{
    for (column=0;column<arr_column;column++)
    {
        fscanf(gain_ptr, " %1f", &MUDG_table[row][column]);
    }
}

fclose(gain_ptr);
问题是我得到的值大约是5.26346e-315DEN
知道我做错了什么吗。这是因为我没有使用EOF吗?

好的,所以在浏览了几篇文章之后,感谢我在这里得到的回复,我更改了代码,使其正常工作

首先,数组应声明为double:

//dynamic allocate array of MUDG_table (1st Dimension)
MUDG_table = calloc(arr_row,sizeof(double *));

//check if the memory has been allocated correctly
if (MUDG_table==NULL) 
{
    printf("Error allocating memory!\n"); //print an error message
    return 1; //return with failure
}

for (cv02=0;cv02<arr_row;cv02++)
{
    MUDG_table[cv02] = calloc(arr_column, sizeof(double));

    //check if the memory has been allocated correctly
    if (MUDG_table[cv02]==NULL) 
    {
        printf("Error allocating memory!\n"); //print an error message
        return 1; //return with failure
    }
}
主要问题是fscanf,我不知道它为什么不工作。所以我用fgets替换了它。我在另一篇文章中发现了这段代码,并对其进行了修改,使其适用于本例

gain_ptr = fopen("MUDG_text.txt", "r");

if (gain_ptr == NULL)
{
    printf("Error Reading File\n");
    return 1; //return with failure
}

for (row=0;row<arr_row;row++)
{
    fgets(buffer, 1024, gain_ptr); //buffer is declared as char buffer[1024];

    tok = strtok(buffer, "\t");    // NULL means 'continue from last token'  
    //tok is declared as char *tok;
    //the numbers in my text file are seperated by tabs
    //so the tokens should be separated by \t

    column = 0;  //since fgets gets a whole line I had to separate the columns
    while (tok != NULL) 
    {
        test_var = atof(tok);  //test variable is declared as double
        //and it is used to hold temporarily the value of tok in double
        tok = strtok(NULL, "\t");    // NULL means 'continue from last token'

        MUDG_table[row][column] = test_var;
        column++;
    }
}   
fclose(gain_ptr);

也许这不是最好的解决方案,但至少它是有效的

MUDG_表=Callocar_行,sizeofint*;这是错误的。您应该始终将x=mallocsizeof*x*…;写入。。。;。在本例中,它是MUDG_table=callocar_行,sizeof*MUDG_table;也许这就是原因:当您想要存储浮点值或双精度值时,sizeofint是错误的。double和int的大小不同。特别是这里的Callocar_柱,sizeofint@不,它应该是MUDG_表[cv02]=callocar_列,sizeof*MUDG_表[cv02];。这样,如果您更改了MUDG_表定义,您就不需要通过所有代码来更改所有这些定义that@Eregrith所有结果均为0.0000。我刚刚在之前的一篇文章中读到:仅供参考,在C中读取浮点数的正确方法是使用fgets和strod。不要为此使用atof或scanf;atof不会告诉您语法错误,scanf会在溢出时触发未定义的行为。有没有可能这就是问题所在?