I am having troubles with I/O to file with 2 dimensional dynamic array. It compiles good but it does not work as I want. Example, I save "map" of numbers 1 and after this I change number in source code to example 5 and compile it, now I load my "map" of numbers 1 but when it writes in cycle for at the end it, the output is 5 not 1. Please can somebody help to fix the code?
#include <iostream>
#include <fstream>
int main()
{
int ** array;
array = new int*[20];
for(int y=0;y<20;y++)
array[y] = new int[30];
for(int y=0;y < 20;y++)
for(int x=0;x < 30;x++)
array[y][x] = 1;
int volba = 1;
std::cin >> volba;
if(volba)
{
std::ifstream in("map",std::ios::in | std::ios::binary);
if(!in.is_open())
std::cout << "in map open error\n";
in.read((char*)&array, sizeof(array));
in.close();
std::cout << "loaded\n";
}
else
{
std::ofstream out("map",std::ios::out | std::ios::binary);
if(!out.is_open())
std::cout << "out map open error\n";
out.write((char*)&array, sizeof(array));
out.close();
std::cout << "saved\n";
}
std::cout << "array\n";
for(int y=0;y < 20;y++)
{
for(int x=0;x < 30;x++)
std::cout << array[y][x] << " ";
std::cout << std::endl;
}
for(int y=0;y<20;y++)
delete [] array[y];
delete [] array;
return 0;
}