简体   繁体   中英

Write a struct to a file

I am working on an assignment, building a persistent b+ tree index on a csv file. I have read in the CSV file, and placed the data that I want to write to the datafile in a deque of structs.

deque<employeeShort> employees

struct employeeShort {
    int Emp_ID;
    string firstname;
    string lastname;
    string SSN;
    string username;
    string password;
};

I now need to write this entire deque to a file (note there are about 10000 entries). But to my understanding I can only write to the file through the buffer which is a char array.

My current solution is to loop through the entire deque and add to a char vector which I can then convert to a character array and use to write to the file.

vector<char> bufferVec;


while(!employees.empty()) {
    readCSV::employeeShort tempEmp = employees.front();
    string tempID = to_string(tempEmp.Emp_ID);
    copy(tempID.begin(), tempID.end(), back_inserter(bufferVec));
    copy(tempEmp.firstname.begin(), tempEmp.firstname.end(), back_inserter(bufferVec));
    copy(tempEmp.lastname.begin(), tempEmp.lastname.end(), back_inserter(bufferVec));
    copy(tempEmp.SSN.begin(), tempEmp.SSN.end(), back_inserter(bufferVec));
    copy(tempEmp.username.begin(), tempEmp.username.end(), back_inserter(bufferVec));
    copy(tempEmp.password.begin(), tempEmp.password.end(), back_inserter(bufferVec));

    employees.pop_front();

}

char buffer[bufferVec.size()];
copy(bufferVec.begin(), bufferVec.end(), buffer);

pageFile.global_fs.write(buffer, sizeof(buffer));

I know this is a very hacky way of doing it and I was hoping someone could suggest something more efficient. Thank you.

If I have well understood, you want to write all the data structures stored in a deque into a file.

For me, you just have to open the file with a std::ofstream and iterate over your deque to write its content into the file.


Example:

C++ code:

#include <fstream>
#include <iostream>
#include <deque>

struct data
{
    char s1;
    std::string s2;
    int s3;
};

int main()
{
    std::deque<data> data_deque;
    data_deque.push_back(data{'A', "Zero", 0});
    data_deque.push_back(data{'B', "One", 1});
    data_deque.push_back(data{'C', "Two", 2});

    std::string file_path("data.txt"); // The path to the file to be written
    std::ofstream out_s(file_path, std::ofstream::app);
    if(out_s)
    {
        for(const data & d : data_deque)
        {
            out_s << "S1: " << d.s1 << '\n';
            out_s << "S2: " << d.s2 << '\n';
            out_s << "S3: " << d.s3 << '\n';
            out_s << std::endl; // separate each data by a new line;
        }

        out_s.close();
    }
    else
        std::cout << "Could not open file: " << file_path << std::endl;

    return 0;
}

Output in data.txt:

S1: A
S2: Zero
S3: 0

S1: B
S2: One
S3: 1

S1: C
S2: Two
S3: 2

When creating the std::ofstream , I have added std::ofstream::app to not erase the previous content in the file, but if you want to clean the file before writing your data, you just have to remove this parameter (by default, it clears the previous content of the file on opening).


I hope it can help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM