Probleme mit IO Operationen



  • Hi !

    Ich habe ein ziemlich blödes Problem:
    Ich möchte Daten in eine Datei schreiben und diese wieder auslesen leider funktioniert das nicht so wie ich es gerne möchte. Ich poste mal kurz den Quellcode der noch nicht sehr lang ist:
    Mapdata.h:

    #ifndef MAPDATA_H
    #define MAPDATA_H
    
    #include "SDL.h"
    
    class MapData {
        public:
        MapData();
        ~MapData();
    
        double version;
        int mapWidth;
        int mapHeight;
        Uint8 mapBpp;
        char* pixelData;
        char* maskData;
    
    };
    
    #endif
    

    Mapdata.cpp:

    #include "MapData.h"
    
    MapData::MapData() {}
    MapData::~MapData() {}
    

    LevelLoader.h:

    #ifndef LEVELLOADER_H
    #define LEVELLOADER_H
    
    #include "SDL.h"
    #include "MapData.h"
    
    class LevelLoader {
        public:
    
        MapData* loadLevel( char* filename );
        void writeLevel( char* filename );
    };
    
    #endif
    

    LevelLoader.cpp:

    #include "LevelLoader.h"
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    MapData* LevelLoader::loadLevel( char* filename ) {
        bool output = true;
    
        MapData* map = new MapData();
        ifstream infile( filename , ifstream::binary | ifstream::in );
    
        infile >> map->version;
        if( output ) cout << "Version: " << map->version << "\n";
    
        infile >> map->mapWidth;
        if( output ) cout << "Map width: " << map->mapWidth << "\n"; 
    
        infile.close();
    
        return map;
    }
    
    void LevelLoader::writeLevel( char* filename ) {
        ofstream outfile( filename , ofstream::binary | ofstream::out );
    
        double version = 1.567;
        outfile << version;
    
        int width = 340;
        outfile << width;
    
        outfile.close();
    
    }
    
    int main( int argc, char** argv ) {
        LevelLoader* levelLoader = new LevelLoader();
        levelLoader->writeLevel("map1.map");
        MapData* map = levelLoader->loadLevel("map1.map");
    }
    

    Okay dann erhalte ich folgende ausgabe wenn ich das in der Konsole ausführe:

    Version: 1.56734
    Map width: 0
    

    Ich habe auch ein kleine testIO geschrieben:
    testIO.h:

    #ifndef TESTIO_H
    #define TESTIO_H
    
    class testIO {
        public:
        void writeDataToFile( char* filename );
        void readDataFromFile( char* filename );
    };
    
    #endif
    

    testIO.cpp:

    #include "testIO.h"
    
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    void testIO::writeDataToFile( char* filename ) {
        ofstream outfile;
        outfile.open( filename , ofstream::out | ofstream::binary );
    
        double testDouble = 0.123;
        outfile << testDouble;
    
        int testInt = 366;
        outfile << testInt;
    
        outfile.close();
    }
    
    void testIO::readDataFromFile( char* filename ) {
        ifstream infile( filename , ifstream::in | ifstream::binary );
    
        double testReadDouble;
        infile >> testReadDouble;
        cout << "testReadDouble: " << testReadDouble << "\n";
    
        int testReadInt;
        infile >> testReadInt;
        cout << "testReadInt: " << testReadInt << "\n";
    
        infile.close();
    
    }
    
    int main( int argc, char** argv) {
        testIO* testio = new testIO();
        testio->writeDataToFile("test.dat");
        testio->readDataFromFile("test.dat");
    }
    

    Hier erhalte ich die Ausgabe:

    testReadDouble: 0.123366
    testReadInt: 366
    

    Wieso funktioniert das nicht so wie ich gerne möchte ?? thx
    Tom


Anmelden zum Antworten