[英]Reading txt file with floats into a 2-D Matrix
I'm trying to read "chol.txt" file with contents: 我正在尝试读取包含内容的“ chol.txt”文件:
6 3 4 8
3 6 5 1
4 5 10 7
8 1 7 25
using the function: (where *ROW and *COL both have the value 4) 使用函数:(其中* ROW和* COL都具有值4)
void ReadFile(float*** MATRIX, int* ROW, int* COL)
{
FILE* openF;
openF = fopen("chol.txt", "r");
for (int incROW = 0; incROW < *ROW; incROW++)
{
for (int incCOL = 0; incCOL < *COL; incCOL++)
{
fscanf(openF, "%f", MATRIX[incROW][incCOL]);
printf("MATRIX[%d][%d] has value %f\n", incROW, incCOL, (*MATRIX)[incROW][incCOL]);
}
}
fclose(openF);
}
but it returns the text before crashing: 但它会在崩溃前返回文本:
MATRIX[0][0] has value 6.000000
MATRIX[0][1] has value 0.000000
MATRIX[0][2] has value 4413697645709019316224.000000
MATRIX[0][3] has value 17751098170076127766626959360.000000
I can manually enter floats into the Matrix, but wanted to read from txt file now. 我可以手动将浮点数输入到Matrix中,但现在想从txt文件读取。 Here is summary of my main():
这是我的main()的摘要:
void ReadFile(float*** MATRIX, int* ROW, int* COL);
int main()
{
int SIZE = 4;
float** M = NULL;
MakeNullMATRIX(&M, &SIZE, &SIZE);
ReadFile(&M, &SIZE, &SIZE);
return 0;
}
As requested MakeNullMATRIX: 根据要求,MakeNullMATRIX:
void MakeNullMATRIX(float*** MATRIX, int* ROW, int* COL)
{
*MATRIX = (float **)malloc(*ROW * sizeof(float*));
for (int i = 0; i < *ROW; i++)
{
(*MATRIX)[i] = (float *)malloc(*COL * sizeof(float));
}
}
Well, sorry, but this looks way too complicated than required... 好吧,对不起,但这看起来太复杂了……
Anyway, without offering too many changes; 无论如何,不提供太多更改; while it's required for you to pass the address of
M
to the MakeNullMATRIX
, you don't have to do so for ReadFile
. 虽然需要将
M
的地址传递给MakeNullMATRIX
,但对于ReadFile
不必这样做。 So... 所以...
1: Changing this line: 1:更改此行:
ReadFile(&M, &SIZE, &SIZE);
like this: 像这样:
ReadFile(M, ... ); // removed &
2: Then this line: 2:然后这行:
void ReadFile(float*** MATRIX, int* ROW, int* COL);
into this: 到这个:
void ReadFile(float ** MATRIX, ... ); // removed one *
3: And then these lines: 3:然后这些行:
fscanf(openF, "%f", MATRIX[incROW][incCOL]);
...
printf("MATRIX[%d][%d] has value %f\n", incROW, incCOL, (*MATRIX)[incROW][incCOL]);
into these: 这些:
fscanf( ... , &( MATRIX[incROW][incCOL]) ); // enclosed it with &( )
...
printf( ... , MATRIX[incROW][incCOL] ); // removed (* )
Should make it work as you'd like it to work, as well as making your code somewhat more friendly to a random reader. 应该使它按您希望的方式工作,并使您的代码对随机阅读器更加友好。
But I seriously cannot understand why not a simple one like this: 但我严重无法理解为什么不像这样简单的一个:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <malloc.h>
void ReadFile( float * MATRIX, int * ROW, int * COL )
{
FILE * openF = fopen( "chol.txt", "r" );
if ( !openF ) {
fprintf( stderr, "chol.txt is missing\n" );
return;
}
for ( int incROW = 0; incROW < *ROW; incROW++ )
{
for ( int incCOL = 0; incCOL < *COL; incCOL++ )
{
fscanf( openF, "%f", MATRIX + (*ROW) * incROW + incCOL );
printf( "MATRIX[%d][%d] has value %f\n", incROW, incCOL, *( MATRIX + (*ROW) * incROW + incCOL ) );
}
}
fclose( openF );
}
int main( )
{
int SIZE = 4;
float * M = malloc( SIZE * SIZE * sizeof( *M ) );
ReadFile( M, &SIZE, &SIZE );
return 0;
}
调用fscanf()
应该像
fscanf(openF, " %f", &((*MATRIX)[incROW][incCOL]));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.