C 编程:从文本文件中逐行读取(双)数字到二维数组 [英] C programming: Read (double) numbers from text file, line by line to a 2D array

查看:53
本文介绍了C 编程:从文本文件中逐行读取(双)数字到二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a text file created with excel that contains a table of dimensions 36x35. The values are double numbers (e.g. 2.58) and the text file looks like this:

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

我知道表的维度(arr_row、arr_column)所以声明一个动态二维数组:

I know the dimensions of the table (arr_row, arr_column) so declare a dynamic 2D array:

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
    }
}

到目前为止一切顺利.然后我尝试从文本文件中读取值并将它们存储到数组中以供进一步处理:

So far so good. Then I try to read the values from the text file and store them to the array for further processing:

//************************************************************************************************************//
//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?

and the problem is that the values I get are something like 5.26346e-315#DEN Any ideas what I am doing wrong. Is it because I do not use the EOF?

推荐答案

好的,所以在浏览了几篇帖子并感谢我在这里得到的回复后,我更改了代码,它可以正常工作.

OK, so after going through several posts and thanks to the responses I got here, I changed the code and it seams to work properly.

首先应该将数组声明为double:

First of all the array should be declared as 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 替换了它.我在另一篇文章中找到了这段代码,并对其进行了修改以适用于这种情况.

The main problem was the fscanf which I have no idea why it is not working. So I replaced it with the fgets. I found this code in another post and I modified it to work in this case.

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);

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

Maybe it is not the best solution, but at least it works.

这篇关于C 编程:从文本文件中逐行读取(双)数字到二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆