I'm trying to use binary files to save my level data for a game. I had it working fine using text files, but I want to use binary files. The functions save the map array, but not the items or enemy array.
here is the kind of data I want to save:
int map[19][550];
ENEMY *enemy = new ENEMY[MAX_ENEMIES];
ITEM *item = new ITEM[MAX_ITEMS];
here are the functions to save/load:
int load_map(char *s) {
int old_max_items, old_max_enemies, i;
ifstream file_in(s, ios::in | ios::binary);
if (!file_in.is_open()) return 1;
file_in.read((char *)map, sizeof(map));
file_in.read((char *)enemy, sizeof(enemy));
file_in.read((char *)item, sizeof(item));
file_in.close();
return 0;
}
int save_map(char *s) {
ofstream file_out (s, ios:
ut | ios::binary | ios::trunc);
if (!file_out.is_open()) return 1;
file_out.write((char *)map, sizeof(map));
file_out.write((char *)enemy, sizeof(enemy));
file_out.write((char *)item, sizeof(item));
file_out.close();
return 0;
}
can anybody tell me why it wont save the enemy/item data, but it will save the map. Does it matter that enemy and item are pointers to struct and that map is a regular array?
here is the kind of data I want to save:
int map[19][550];
ENEMY *enemy = new ENEMY[MAX_ENEMIES];
ITEM *item = new ITEM[MAX_ITEMS];
here are the functions to save/load:
int load_map(char *s) {
int old_max_items, old_max_enemies, i;
ifstream file_in(s, ios::in | ios::binary);
if (!file_in.is_open()) return 1;
file_in.read((char *)map, sizeof(map));
file_in.read((char *)enemy, sizeof(enemy));
file_in.read((char *)item, sizeof(item));
file_in.close();
return 0;
}
int save_map(char *s) {
ofstream file_out (s, ios:
if (!file_out.is_open()) return 1;
file_out.write((char *)map, sizeof(map));
file_out.write((char *)enemy, sizeof(enemy));
file_out.write((char *)item, sizeof(item));
file_out.close();
return 0;
}
can anybody tell me why it wont save the enemy/item data, but it will save the map. Does it matter that enemy and item are pointers to struct and that map is a regular array?