Ints und bools Binär lesen/schreiben

liggi

Grünschnabel
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 Lesen, konnte ich noch nicht testen, da ich bis jetzt keine Datei geschrieben bekommen habe:

Code:
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;
}

Der Code zum Schreiben:


Code:
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;
}

Danke im voraus.

mfg liggi

Edit: Lösung s. Hier
 
Zuletzt bearbeitet:
Zurück