gpt4 book ai didi

C编程: Read (double) numbers from text file,逐行写入二维数组

转载 作者:行者123 更新时间:2023-11-30 17:15:42 24 4
gpt4 key购买 nike

我有一个用 Excel 创建的文本文件,其中包含尺寸为 36x35 的表格。这些值是 double 字(例如 2.58),文本文件如下所示:

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

我知道表的尺寸(arr_row、arr_column),因此声明一个动态二维数组:

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

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

关于C编程: Read (double) numbers from text file,逐行写入二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29897609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com