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

时间:2018-06-29 09:34:56

标签: c++

我有一个像这样的数据文件

               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

我需要读取文件并将前三行存储到矩阵中

                  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)

请,我们将不胜感激!

2 个答案:

答案 0 :(得分:1)

如果我对您的理解是正确的,那么在文件打开时是否需要跳过第一个矩阵数据

// 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 . . .

答案 1 :(得分:0)

顺着您的代码风格(由于容易出错的new[]delete这样,我不建议这样做),那么一种方法(不是唯一的方法)是只需循环两次即可,每个矩阵一次。第二个循环将继续从其停止处提取双精度,而空的换行符将被忽略。

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[],而是使用{{1 }}。