I'm working on a simple program with a friend. I wrote a few I/O functions to plug into the program (they just read from/write to txt files). I'm planning to add them in a header file to be included in the main program.
The program manages collections of items by doing a few CRUD operations. For now, it only stores into memory. There are 3 different
structs (to represent the different kind of objects) and 3
vectors (one for each type of struct) to represent the 3 collections.
For each vector of a different struct type (which I've called t_item in this example) I made three I/O functions:
void serialize_item(const string &file_name, t_item &an_item)
Declares a ofstream variable and appends (ios::app) each item field to the txt file. Then calls file.close()
void serialize_all_items(const string &file_name, vector<t_item> &items)
Declares a ofstream variable and overwrites (ios::trunc) file contents. Iterates through vector, writing each struct field to the txt file. Then calls file.close()
void deserialize_items(const string &file_name, vector<t_item> &items)
Declares a local t_item variable and a ifstream variable. Iterates reading the file contents and saving each item field to the t_item variable. When an item is complete, it calls push_back to save it into the vector and goes back to reading the next item from file. Then calls file.close()
The thing is: me and my friend are working separately on different parts of the code. Since he declared all the structs, he got to decide what name to use for each struct and what are the names of the fields (although we both agreed there would be a certain amount of fields in each struct and they would be of certain type, since that's the design we need to implement).
My question now is: is there a way for me to implement the I/O functions without knowing the name of the structs and fields he used? For example, this function:
1 2 3 4 5 6 7 8
|
void serialize_person(const string &file_name, t_person &a_person)
{
ofstream personsFile(file_name, ios::app);
personsFile<< a_person.name << endl;
personsFile<< a_person.age << endl;
personsFile<< a_person.gender << endl;
personsFile.close();
}
|
My friend could have chosen to name the struct "onePerson" instead of "t_person" and the fields could be "fullName", "age", "sex". But the structure is pretty much the same.
How can I tweak my I/O functions to fit the same structure despite the struct/field names the other programmer chose?