如何从文件中读取 100x24 矩阵并将其作为浮点矩阵保存在内存中?
How to read a 100x24 matrix from file and keep it in memory as a float matrix?
这是我的代码,我可以将数据保存在字符矩阵中,但我想将其保存在浮点矩阵中
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;
}
如果你在互联网上搜索"c ++ read file matrix",你可能会看到这样的代码:
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;
}
}
您可以通过将数据类型从double
更改为float
来读取float
矩阵。
相关文章:
- C++,您能否设计一种数据结构,将指针保存在连续内存中并且不会使它们失效?
- 是否可以通过每次在内存中仅保存一个平铺来处理完整的平铺 tiff 图像?
- 如何从文件中读取 100x24 矩阵并将其作为浮点矩阵保存在内存中?
- 如果我不将预先编写的字符串保存到变量,它还在内存中吗?
- 将内存保存到文件并加载它而不必解析数据?
- 声明变量以保存字符串列表时的内存分配
- 我的共享内存对象保存在哪里
- 在多次运行C++程序时将巨大的矩阵保存在内存中
- 在编译时检查特征类型保存的数据在内存中是否连续
- 局部变量保存相同的内存地址
- 读取.txt文件保存在内存中
- 保存未知数量的整数,而不会花费太多时间/内存
- 将它们(函数)保存在内存中的同一位置
- 什么设计模式将数据部分保存在持久存储 (SQL) 中,部分保存在 RAM(内存)中
- 将图像保存到内存
- GDI - 是否可以在WM_PAINT调用之间将位图保存在内存中
- C++ 如何将内存中的文件流保存到另一个位置
- 使用位字段保存内存
- 将Mat压缩到Jpeg中,并将结果保存到内存中
- WinBio保存捕获样本为图像-无法从内存中读取