c - 将带有 float 的txt文件读入二维矩阵

标签 c loops pointers matrix floating-point

我正在尝试读取“chol.txt”文件,其内容为:

6 3 4 8
3 6 5 1
4 5 10 7
8 1 7 25

使用函数:(其中 *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);
}

但它返回崩溃前的文本:

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

我可以手动将 float 输入到矩阵中,但现在想从 txt 文件中读取。这是我的 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;
}

根据要求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));
}
}

最佳答案

好吧,抱歉,但这看起来比要求的复杂得多......

无论如何,不​​提供太多改变;虽然您需要将 M 的地址传递给 MakeNullMATRIX,但对于 ReadFile 则不必这样做。所以...

1:更改此行:

ReadFile(&M, &SIZE, &SIZE);

像这样:

ReadFile(M, ... );  // removed &

2:然后这一行:

void ReadFile(float*** MATRIX, int* ROW, int* COL);

进入此:

void ReadFile(float ** MATRIX, ... );  // removed one *

3:然后这些行:

fscanf(openF, "%f", MATRIX[incROW][incCOL]);
...
printf("MATRIX[%d][%d] has value %f\n", incROW, incCOL, (*MATRIX)[incROW][incCOL]);

分为这些:

fscanf( ... , &( MATRIX[incROW][incCOL]) );  // enclosed it with &( )
...
printf( ... , MATRIX[incROW][incCOL] );  // removed (* )

应该让它按照您希望的方式工作,并使您的代码对随机读者更加友好。

但我真的无法理解为什么不是像这样简单的一个:

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

关于c - 将带有 float 的txt文件读入二维矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21963601/

相关文章:

javascript - 获取对象中元素的路径,而不循环遍历所有元素(第二层次结构级别未知)

Javascript:将 Console.debug() 输出写入浏览器?

C++ 现代字符串指针

c++ - 指定给 RtlValidateHeap 的地址无效

c - C 中的 limit.h *_MAX 常量的正确说明符是什么?

c - 内核目录中的 qstr 结构是否包含 Linux 文件的文件名?

c - 为节点指针分配空间

Java - 读取工资并获取总计

c - 功能参数差异

c - 简单的 Makefile : undefined reference to symbol 'cos@@GLIBC_2.2.5' 问题