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

我有一个用excel创建的文本文件,其中包含一个尺寸为36×35的表。 值是双数(例如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吗?

好的,所以经过几个post并感谢我在这里得到的回复后,我改变了代码并使接缝正常工作。

首先,数组应声明为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 

主要的问题是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 

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