0

構造体の配列をバイナリ ファイルに保存する必要があります。しかし、問題があります!構造体にベクトルの 3 次元配列があります。これは私の構造体です:

struct State
{
    vector<bool> ***value;
    vector<int> ***rfcCLB2Bits;
    State()
    {
      new3dArray(value , 60 , 100 , 2);
      new3dArray(rfcCLB2Bits , 60 , 100 , 2);
    }

};

new3dArray 関数を見たいと思うかもしれません:

template <class T>
void new3dArray(T ***&u3d, int nx , int ny , int nz)
{
    u3d = new T ** [nx];
    if (NULL == u3d)
    {
      cout <<"erro3"<<endl;
    }
    else
    {
        for (int i = 0; i < nx; i++)
        {
            u3d[i] = new T *[ny];
            if (NULL == u3d[i])
            {
                cout <<"erro4"<<endl;
            }
            else
            {
                for (int j = 0; j < ny; j++)
                {
                    u3d[i][j] = new T [nz];
                    if (NULL == u3d[i][j])
                    {
                        cout <<"erro5"<<endl;
                    }
                }
            }
        }
    }
}

最後に、保存と読み込みのための私のコード:

int main()
{
    State *states;
    int k ;
    cin >> k;
    states = new State[k];

    ...

    string location = "save1.bin";
    ofstream fs(location, std::ios::out | std::ios::binary | std::ios::app);
    fs.write(reinterpret_cast<const char *>(states), sizeof(states));
    fs.close();

    ...

    ifstream file (location, ios::in|ios::binary|ios::ate);
    if (file.is_open())
    {
        file.seekg (0, ios::beg);
        file.read ((char*)&states, sizeof(states));
        file.close();
    }

    ...
}

しかし、うまくいきませんでした!私は何をしなければなりませんか?

4

0 に答える 0