从文件中读取选定的行范围并存储到数组中
Read from file a selected range of lines and store into arrays
我有一个这样的数据文件
1.2 0.07 0.3
2.3 1.0 1.1
0.3 2.2 1.1
12.2 0.0 0.0
0.0 20.1 0.0
1.2 0.07 0.3
2.3 1.0 1.1
0.3 2.2 1.1
我需要读取文件并将前 3 行存储到矩阵中
1.2 0.07 0.3
A = 2.3 1.0 1.1
0.3 2.2 1.1
并从第 4 行到末尾进入另一个矩阵
12.2 0.0 0.0
0.0 20.1 0.0
B = 1.2 0.07 0.3
2.3 1.0 1.1
0.3 2.2 1.1
到目前为止,我只能创建第一个矩阵
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
const char * filename = "data.txt";
ifstream is(filename);
if (!is)
{
cout << "Unable to open file: " << filename << endl;
return 1;
}
int height = 3, width = 3;
// Dynamic array
double *vec;
vec = new double*[height];
for (int i = 0; i<height; i++)
vec[i] = new double[width];
// Read the data
for (int i = 0; i<height; i++)
for (int j=0; j< width; j++)
is >> vec[i][j];
if (!is)
cout << "Error reading into array" << endl;
// Display the data
for (int i = 0; i<height; i++)
{
for (int j=0; j< width; j++)
cout << setw(5) << vec[i][j];
cout << endl;
}
cout << "done" << endl;
// Delete the array
for (int i = 0; i<height; i++)
delete [] vec[i];
delete [] vec;
return 0;
}
那么如何跳到第 5 行创建矩阵 B? 我想要一个类似循环的东西(矩阵 A 从 0 到 3,矩阵 B 从 4 到 8(
请,任何帮助将不胜感激!
如果我理解正确,如果您需要在文件刚打开时跳过第一个矩阵数据
// Skip the data
double dummy;
for (int i = 0; i<height; i++)
for (int j=0; j< width; j++)
is >> dummy;
(高度和宽度适用于跳过的矩阵 A(
如果必须在矩阵 A 之后立即读取矩阵 B,则无需跳过任何内容
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
const string filename = "data.txt";
ifstream is(filename);
if (!is)
{
cout << "Unable to open file: " << filename << endl;
return 1;
}
int height = 3, width = 3;
// Dynamic array
vector<double> A(height * width);
// Read the data
for (int i = 0; is && i < height; ++i)
for (int j = 0; j < width; ++j)
is >> A[i * width + j];
if (!is)
cout << "Error reading into array" << endl;
// Display the data
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
cout << setw(5) << A[i * width + j];
cout << endl;
}
cout << endl;
// Delete the array
A.clear();
// reading B
height = 5;
// Dynamic array
vector<double> B(height * width);
// Read the data
for (int i = 0; is && i < height; ++i)
for (int j = 0; j < width; ++j)
is >> B[i * width + j];
if (!is)
cout << "Error reading into array" << endl;
// Display the data
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
cout << setw(5) << B[i * width + j];
cout << endl;
}
cout << endl;
B.clear();
cout << "done" << endl;
return 0;
}
输出
1.2 0.07 0.3
2.3 1 1.1
0.3 2.2 1.1
12.2 0 0
0 20.1 0
1.2 0.07 0.3
2.3 1 1.1
0.3 2.2 1.1
done
Press any key to continue . . .
坚持你的代码风格,(我不推荐,因为容易出错new[]
'ing和delete
'ing(,那么一种方法,而不是唯一的方法,就是简单地循环精确地循环2次迭代,每个矩阵一次。第二个循环将继续从中断的地方提取双精度值,空换行符将被忽略。
int height = 3, width = 3;
for (int i = 0; i < 2; ++i)
{
// Dynamic array ...
// Read the data ...
// Display the data ...
// Delete the array ...
height = 5;
}
如果你的下一个问题是如果我不知道还有 5 行,但可能是 6 行、10 行或 423 行怎么办,那么答案将是不要使用new double[]
而是更喜欢std::vector
.
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 将字符串存储在c++中的稳定内存中
- std::原子加载和存储都需要吗
- 尝试通过多个向量访问变量时,向量下标超出范围
- 在函数内创建的对象的范围 - 如果在函数外部存储和访问引用,它们是否有效?
- 当我尝试将范围值存储为 8 位的固定宽度整数时,它向我显示一些其他值 [ASCII]
- 从文件中读取选定的行范围并存储到数组中
- 数组不会存储超出第一个空间的范围
- 指向本地变量的指针存储在此变量范围之外
- 如果存储原始参考的类不在范围之外,可以捕获会员参考安全吗?
- 为什么免费存储区中对象的生存期绑定到范围
- 存储任何值和特定类范围的值
- 地图的范围和价值存储问题
- 存储lambda函数,该函数将范围变量捕获为类成员以供重用
- OpenCV C++:从存储在 int 向量的索引中为垫子类型数据创建列范围
- 为什么存储在 STL 向量中的堆栈分配元素C++即使超出范围也仍然处于活动状态
- float变量如何存储不在其范围内的负值
- 如何使用存储在列表中的特定元素范围初始化向量
- 存储一组不重叠的范围,并严格查找一个值是否存在于任何一个范围中
- 在映射中存储一定范围内的值