从文件中读取选定的行范围并存储到数组中

Read from file a selected range of lines and store into arrays

本文关键字:范围 存储 数组 文件 读取      更新时间:2023-10-16

我有一个这样的数据文件

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.