Ints und bools Binär lesen/schreiben
-
Hallo Leute,
ich hab ein Problem, nämlich will ich ints und bools binär schreiben und lesen. Wenn ich cast benutze stürzt das Programm ab. Bis jetzt hab ich aber nur Beispiele mit casts gefunden.
Der Code zum Schreiben:int MapWrite(char** map, bool** shotable, int x, int y, std::string out) { std::ofstream file(out.c_str(), std::ios::out | std::ios::binary); if(!file) { return -1; } for(int i=0; i<x; i++) { for(int a=0; a<y; a++) { file.write((char*)&map[x][a], sizeof(map[x][a])); //Hier stürzt er ab //file.write(reinterpret_cast < char * > (&map[x][y]), sizeof(map[x][y])); file.write((char*)&shotable[x][a], sizeof(shotable[x][a])); //file.write(reinterpret_cast < char * > (&shotable[x][y]), sizeof(shotable[x][y])); } } return 0; }Der Code zum Lesen, konnte ich noch nicht testen, da ich bis jetzt keine Datei geschrieben bekommen habe:
int Map::Load(std::string map) { std::string image; std::string tmp; std::ifstream file(map.c_str(), std::ios::in | std::ios::binary); if(!file) { return -1; } map_m = new int*[map_x]; shotable_m = new bool*[map_x]; for(int x=0; x<map_x; x++) { map_m[x] = new int[map_y]; shotable_m[x] = new bool[map_y]; for(int y=0; y<map_y; y++) { file.read((char*)&map_m[x][y],sizeof(map_m[x][y])); //int file.read((char*)&shotable_m[x][y],sizeof(shotable_m[x][y])); //bool } } file.close(); return 0; }Danke im voraus.
mfg liggi
-
hat keiner eine Idee, warum dass nicht geht?
mfg liggi
-
Zeig doch mal, wie die Variable "map" gefüllt wird. Und der cast bei
(char*)&map[x][a]ist erstens unnötig, weil&map[x][a]schon einchar*ist und zweitens ist es ein C-Cast, der in C++ nichts zu suchen hat (siehe static_cast/reinterpret_cast, letzeren sollte man wann immer möglich vermeiden).edit: Wieso übergibst du bei MapWrite einen char** und keinen int** ? Und: Wieso nimmst du nicht std::vector?
-
Hier mal die überarbeitete Version vom Schreiben:
#include <iostream> #include <vector> #include <fstream> using namespace std; int MapWrite(vector<vector<int> > map, vector<vector<bool> > shootable, int x, int y, std::string out) { std::ofstream file(out.c_str(), std::ios::out | std::ios::binary); if(!file) { return -1; } for(int i=0; i<x; i++) { for(int a=0; a<y; a++) { file.write(reinterpret_cast < char * > (&map[x][a]), sizeof(map[x][a])); //Stürzt hier ab //file.write(static_cast < char * > (&map[x][a]), sizeof(map[x][a])); //Ergibt Fehlermeldung file.write(reinterpret_cast < char * > (&shootable[x][a]), sizeof(shootable[x][a])); //file.write(static_cast < char * > (&shootable[x][a]), sizeof(shootable[x][a])); } } return 0; } int main() { vector<vector<int> > map; vector<vector<bool> > shootable; vector<int> temp; vector<bool> temp2; temp.resize(100, -1); temp2.resize(100, true); map.resize(100, temp); shootable.resize(100, temp2); for(int x=0; x<100; x++) { for(int y=0; y<100; y++) { if(y<40) map[x][y]=0; else map[x][y]=1; shootable[x][y]=true; } } return MapWrite(map, shootable, 100, 100, "../map.map"); }Lesen:
int Map::Load(std::string map) { std::string image; std::string tmp; std::vector <int> temp; std::vector <bool> temp2; std::ifstream file(map.c_str(), std::ios::in | std::ios::binary); if(!file) { return -1; } temp.resize(map_x, -1); temp2.resize(map_x, true); map_m.resize(map_y, temp); shootable_m.resize(map_y, temp2); for(int x=0; x<map_x; x++) { for(int y=0; y<map_y; y++) { file.read(reinterpret_cast < char * > (&map_m[x][y]),sizeof(map_m[x][y])); file.read(reinterpret_cast < char * > (&shootable_m[x][y]),sizeof(shootable_m[x][y])); } } file.close(); return 0; }mfg liggi
-
Was sagt denn der Debugger?
-
ich hab den Fehler beim Schreiben gefunden, beim Zugriff auf den Vector/Array wird die Grenze um eines überschritten.
Der richtige Code:
int MapWrite(vector<vector<int> > map, vector<vector<bool> > shootable, int x, int y, std::string out) { std::ofstream file(out.c_str(), std::ios::out | std::ios::binary); if(!file) { return -1; } for(int i=0; i<x; i++) { for(int a=0; a<y; a++) { file.write(reinterpret_cast < char * > (&map[i][a]), sizeof(map[i][a])); //i statt x, x ist z.B. 100 und der größte Index ist 99 file.write(reinterpret_cast < char * > (&shootable[i][a]), sizeof(shootable[i][a])); } } return 0; }Vielen Dank
mfg liggi