Write and load vector of structs in a binary file

2020-04-30 03:32发布

问题:

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

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

    struct Record{
        vector<Field> structure;
        vector<string> info;
    };

What I want to do is to store a vector of my struct Record inside a binary file and to successfully load it back. The problem is that my struct has two vectors inside of it and they are causing me some trouble. Can you help me out?

回答1:

I know it's a duplicate but meh.. I was bored. You basically just write functions that would write the structure to a stream. First you write the size of the structure if it's a POD.

If it's not POD, you write the size of each element then you write the data for the element.

Below you can see for writevecfield, it writes the size of the vector first. Then it writes the entire POD structure to the stream. writesvecstring writes the size of the vector first. Then it goes through the vector and writes: the size of each string followed by its contents.

To read, you do the opposite. readvecfield reads the size of the vector from the file because it was the first thing written. After reading it, we resize the vector and read "size" amount of "Field" structures into the new vector.

To read the strings, we do the opposite as well. readvecstring reads the size of the vector from the file first. It resizes the vector to the read size. Next it loops "size" amount of times because that's the amount of strings in the file.

We then read the size of the string, resize a string and read the contents into that string. We then add that to the vector and move onto the next string: read the size first, resize a string, read contents, add to vector..

#include <fstream>
#include <vector>
#include <iostream>
#include <sstream>

using namespace std;


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

struct Record
{
    vector<Field> structure;
    vector<string> info;
};

void writevecfield(ostream& os, const vector<Field> &vec)
{
    typename vector<Field>::size_type size = vec.size();
    os.write((char*)&size, sizeof(size));
    os.write((char*)&vec[0], vec.size() * sizeof(Field));
}

void readvecfield(istream& is, vector<Field> &vec)
{
    typename vector<Field>::size_type size = 0;
    is.read((char*)&size, sizeof(size));
    vec.resize(size);
    is.read((char*)&vec[0], vec.size() * sizeof(Field));
}

void writevecstring(ostream& os, const vector<string> &vec)
{
    typename vector<string>::size_type size = vec.size();
    os.write((char*)&size, sizeof(size));

    for (typename vector<string>::size_type i = 0; i < size; ++i)
    {
        typename vector<string>::size_type element_size = vec[i].size();
        os.write((char*)&element_size, sizeof(element_size));
        os.write(&vec[i][0], element_size);
    }
}

void readvecstring(istream& is, vector<string> &vec)
{
    typename vector<string>::size_type size = 0;
    is.read((char*)&size, sizeof(size));
    vec.resize(size);

    for (typename vector<string>::size_type i = 0; i < size; ++i)
    {
        typename vector<string>::size_type element_size = 0;
        is.read((char*)&element_size, sizeof(element_size));
        vec[i].resize(element_size);
        is.read(&vec[i][0], element_size);
    }
}



void WriteRecord(ostream& out, const Record& r)
{
    writevecfield(out, r.structure);
    writevecstring(out, r.info);
}

void ReadRecord(istream& in, Record& r)
{
    readvecfield(in, r.structure);
    readvecstring(in, r.info);
}


int main()
{
    Record R;

    Field first = {"HELLO", 1, 20};
    Field second = {"WORLD", 2, 40};
    R.structure.push_back(first);
    R.structure.push_back(second);
    R.info.push_back("INFO FOR HELLO");
    R.info.push_back("INFO FOR WORLD");

    std::ofstream out("C:/Users/***/Desktop/Test.bin", std::ios::out | std::ios::binary);
    WriteRecord(out, R);
    out.close();

    Record RR;
    std::ifstream in("C:/Users/***/Desktop/Test.bin", std::ios::in | std::ios::binary);
    ReadRecord(in, RR);
    in.close();

    for (int i = 0; i < RR.structure.size(); ++i)
    {
        std::cout<<"Name:   "<<RR.structure[i].name<<"\n";
        std::cout<<"Type:   "<<RR.structure[i].type<<"\n";
        std::cout<<"Length: "<<RR.structure[i].length<<"\n";
        std::cout<<"INFO:   "<<RR.info[i]<<"\n\n";
    }
}


回答2:

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<Field> &vec)
{
    unsigned int nSize = vec.size();
    if (nSize != fwrite(&vec[0],sizeof(Field),nSize,fpOut))
         return false;
    else return true;
}