C++如何读取.dat文件,并放在一个数组中。

首先采用QSGS方法生成了一个文件,现在想只读取这个文件中的数据并把它放在一个数组中,每行的第一个数是x,第二个数是y,第三个数是z。以用于后续的计算。不知如何实现。下面有已有的代码,但是读取不了。

img

for (m = 1; m < 11; m++)
    {
        ostringstream os;
        string s_head = "./2D_";

        os << m;
        string s_tail = ".dat";

        string s = s_head + os.str() + s_tail;
        s = ("C:\\特征\\QSGS_%d.dat", m);
        char* c = const_cast<char*>(s.c_str());  //指针变量
        ifstream ifs(c, ios::in); //以输入的方式打开文件,file的名称是ifs
        if (!ifs)
        {
            cout << "no data" << endl;
            system("pause");
            exit(-1);
        }

        int head = 1;
        string line;
        while (getline(ifs, line))// line中不包括每行的换行符  
        {
            double x, y, p;
            int i, j;
            istringstream is(line);
            if (head <= 3)
            {
                head++;
                continue;
            }
            is >> x >> y >> p;
            i = int(x);
            j = int(y);
            arrgrid[i][j] = p;
        }
        outputQSGS(m);
    }

#include <iostream>
#include <vector>
#include <array>
#include <fstream>
#include <limits>

using namespace std;

int main()
{
    ifstream file("test.dat");
    if (!file)
    {
        cerr << "failed to read file\n";
        return 1;
    }
    for (int i = 0; i < 3; i++)
        file.ignore(numeric_limits<streamsize>::max(), '\n');
    vector<array<int, 3>> data;
    int x, y, z;
    while (file >> x >> y >> z)
        data.push_back({x, y, z});
    for (auto [x, y, z] : data)
        cout << x << ' ' << y << ' ' << z << '\n';
    return 0;
}