簡體   English   中英

C編程:從文本文件中逐行讀取(雙精度)數字到2D數組

[英]C programming: Read (double) numbers from text file, line by line to a 2D array

我有一個用excel創建的文本文件,其中包含尺寸為36x35的表格。 值是雙數(例如2.58),文本文件如下所示:

1.25  2.31 ...
4.28  2.56 ...
3.27  ...
...   ...

我知道表的尺寸(arr_row,arr_column),因此聲明一個動態2D數組:

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-315#DEN任何想法我做錯了。 是因為我不使用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);

也許這不是最好的解決方案,但至少它能起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM