Write and load vector of structs in a binary file c++

前端 未结 2 1218
野性不改
野性不改 2020-12-22 02:22

I really need your help. I have the following structs in my code:

    struct Field{
        char name[20];
        int type;
        int length;
    };

             


        
2条回答
  •  一向
    一向 (楼主)
    2020-12-22 02:42

    I used c-like sentence to process. The key is you just need to find the address of the first vec data, than use adjacent buffer to write them in files.

    bool storeStructVec(FILE *fpOut, const vector &vec)
    {
        unsigned int nSize = vec.size();
        if (nSize != fwrite(&vec[0],sizeof(Field),nSize,fpOut))
             return false;
        else return true;
    }
    

提交回复
热议问题