C++ Read and write multiple objects of same class

前端 未结 4 833
故里飘歌
故里飘歌 2021-01-13 06:52
airport air(1,2,3); //an airport constructor
ofstream myfile;
myfile.open(\"rishab\",ios::app||ios::binary);
myfile.write((char*)air,sizeof(airport);
myfile.close();         


        
相关标签:
4条回答
  • 2021-01-13 07:36

    What you are trying to do is serialization. This way of serializing objects is not stable, and highly depends on what airport is. It's better to use explicit serialization.

    Here's a description of what serialization is and why is it made this way.

    In MessagePack a typical serialization-deserialization scenario would look like this:

    struct airport {
     std::string name; //you can name your airports here
     int planeCapacity;
     int acceptPlanesFrom;
     MSGPACK_DEFINE(name,planeCapacity,acceptPlanesFrom);
    };
    

    ...

    // define your airports
    std::vector<airport> airports;
    airport a={"BLA",1,2};
    airport b={"BLB",3,4};
    airports.push_back(a);
    airports.push_back(b);
    
    // create a platform-independent byte sequence from your data
    msgpack::sbuffer sbuf;
    msgpack::pack(sbuf, airports) ;
    std::string data=sbuf.data();//you can write that into a file
    
    msgpack::unpacked msg;
    // get your data safely back
    msgpack::unpack(&msg, sbuf.data(), sbuf.size());
    msgpack::object obj = msg.get();
    
    std::cout<<obj<<std::endl;
    
    // now convert the bytes back to your objects
    std::vector<airport> read_airports;
    obj.convert(&read_airports);
    std::cout<<read_airports.size()<<std::endl;
    

    with the console output:

    [["BLA", 1, 2], ["BLB", 3, 4]]
    2
    
    • some more
    0 讨论(0)
  • 2021-01-13 07:39
    ofstream myfile;
    
    std::vector<airport> vec;
    myfile.open("rishab",ios::app||ios::binary);
    while(myfile.write(reinterpret_cast<char*>(&air),sizeof(airport)) != 0)
       vec.push_back(air);
    
    myfile.close();
    

    Now use vec for processing

    0 讨论(0)
  • 2021-01-13 07:42

    You can program it like this.

    struct AirPort
    {
        int a;
        int b;
        int c;
    };
    int main()
    {
        std::vector<AirPort> airportList;
        FILE* fp = fopen(filename,"rb");
        if( NULL != fp)
        {
            while(!feof(fp))
            {
                AirPort ap;
                if (fread(&ap,sizeof(ap),1,fp)==1)
                {
                    airportList.push_back(ap);
                }
            }
        }
        fclose(fp);
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-13 07:47

    Well, if you're sure that your file is valid, then you can simply use read() until you reach EOF. Each read() - of sizeof(airport)- will give you a valid airport object.

    Note that storing the binary "value" of and object will result in an invalid object when loading it if it contains pointers - or references.

    EDIT: myfile.write((char*)&air,sizeof(airport); will write the content of the air object the file. By doing this, you're actually writing the object, not the pointer.

    0 讨论(0)
提交回复
热议问题