Reconstructing a vector saved in a binary file..
I've stumbled on this little example: vector::reserve - C++ Reference
What I'm trying to do is stick a bunch of employee objects in a vector, save it to a file, and load the file and use the vector next time I open the program, a database of sorts.
Code:
void save_data()
{
ofstream employee_data;
employee_data.open("employee_data.bin", ios::binary);
if (employee_data.is_open())
{
cout << "File successfully opened and initialized." << endl;
if(!employees.empty())
{
employee_data.write(reinterpret_cast<char*>&employees[0], employees.size() * sizeof(employee));
cout << "Data successfully written to the file." << endl;
}
else
{
cout << "That vector is empty bro...." << endl;
}
}
else
{
cout << "This didn't work noob!" << endl;
}
}
void get_data()
{
ifstream employee_data;
employee_data.open("employee_data.bin", ios::binary);
if(employee_data.is_open())
{
//start copying data to memory
}
else
{
//something went wrong, probably can't find file, or doesn't exist
}
}
I'm not 100% sure how to go about copying the data from the file so that I can use the data in memory like I would any other vector.