C++ 如何从文件中读取100x24矩阵并将其作为浮点矩阵保存在内存中?

C++ 如何从文件中读取100x24矩阵并将其作为浮点矩阵保存在内存中?,c++,C++,这是我的代码,我可以将数据保存在char矩阵中,但我想将其保存在float矩阵中 const unsigned int HEIGHT = 100; const unsigned int WIDTH = 24 ; char arr[HEIGHT][WIDTH]; ifstream fin; fin.open("PW.txt"); string line; //let's assume here the proper size of input Map for (unsigned int

这是我的代码,我可以将数据保存在char矩阵中,但我想将其保存在float矩阵中

const unsigned int HEIGHT = 100;
const unsigned int WIDTH = 24 ;



char arr[HEIGHT][WIDTH];

ifstream fin;
fin.open("PW.txt");
string line;

//let's assume here the proper size of input Map
for (unsigned int i = 0; i < HEIGHT; i++)
{
    getline(fin, line);
    for (unsigned int j = 0; j < WIDTH; j++)
    {
        arr[i][j] = line[j];
    }
}

//let's assume here the proper size of input Map
for (int i = 0; i < HEIGHT; i++)
{
    for (int j = 0; j < WIDTH; j++)
    {
        cout << arr[i][j]  ;
    }
    cout << endl;
}
const unsigned int HEIGHT=100;
常量无符号整数宽度=24;
字符arr[高度][宽度];
流鳍;
财务公开(“PW.txt”);
弦线;
//让我们在这里假设输入映射的正确大小
for(无符号整数i=0;icout如果您在internet上搜索“c++读取文件矩阵”,您可能会看到如下代码:

static const int MAX_ROWS = 100;
static const int MAX_COLUMNS = 24;

//...
double matrix[MAX_ROWS][MAX_COLUMNS];
double value = 0.0;
for (int row = 0; row < MAX_ROWS; ++row)
{
    for (int column = 0; column < MAX_COLUMNS; ++column)
    {
        text_file >> value;
        matrix[row][column] = value;
    }
}
static const int MAX_ROWS=100;
静态常量int MAX_COLUMNS=24;
//...
双矩阵[最大行数][最大列数];
双值=0.0;
对于(int row=0;row>值;
矩阵[行][列]=值;
}
}

您可以通过将数据类型从
double
更改为
float

来读取
float
的矩阵,您可以显示文件的摘录,这样我们就可以看到它是什么样子了吗?您是如何保存文件的?“我可以将数据保存在char矩阵中,但我想将其保存在float矩阵中”-那么到底是什么问题?你到底在问什么?你尝试了什么?在哪些方面不起作用?我不明白你的问题:你希望如何从字符转换为浮点?据我所知,输入文件只包含24个字符的行。我使用Matlab将100x24矩阵保存在文本文件中。现在我想读取这些数据在VisualStudioC++中将其保存在二维矩阵中,我想从文本文件中读取这个矩阵,并将其保存在浮点型2D矩阵中。在我的代码中,我可以将它保存在字符矩阵中,但是我想将它保存在浮点矩阵中,每个列都用‘/t’和行’/n’分隔。最好先定义<代码>文本文件> /Cord>变量,使其更加清晰;