Writing and Reading .dat file from integer multidimensional array c++

I have an integer multidimensional array of commands. I want it to be written into a .dat file, then later in the program, it will be read and the data written will be used for other parts of the program.

static int commands[][2]= {{5,1}, {2,0}, {5,4}, {3,0}, {5,1}, {3,0}};
//create file if first time
    ofstream oFile("data.dat", ios::out);
    if(!oFile){
        cerr << "Error creating file!" << endl;
        exit(1);
    }

for(i=0; i<86; i++){
        for(int j=0; j<2; j++){
            oFile << commands[i,j] << ' ' << endl;
        }
}



//reading from file

    ifstream iFile("data.dat", ios::in);
    if(!iFile){
        cerr << "Error reading file!" << endl;
        exit(1);
    }

    for(i=0; i<86; i++){
        for(int j=0; j<2; j++){
            iFile >> commands[i,j];
        }
    }

I am successful in making the file, but I have no idea on how to call or what to call during reading.

Error for reading the file:

2.cpp(135) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'int [2]' (or there is no acceptable conversion)

Thanks a lot for the help!


Source: c++

Leave a Reply