To serialize files, how would I do this? Would I need to create a function to loop through file names and serialize that to one file?
To serialize files, how would I do this? Would I need to create a function to loop through file names and serialize that to one file?
By default, std::ofstream is opened in output mode, which amongst other things means the file will be truncated to zero length when it is opened. You can control this behaviour via bit flags when opening files:
https://en.cppreference.com/w/cpp/io/ios_base/openmode
In this case, try opening subsequent output files the following way and see if it solves your problem, as adding the "app" flag will allow you to append to a file, rather than overwriting it:
std::ofstream out{ "Categories.txt", std::ios_base::out | std::ios_base::app };
Another possibility is to add the "in" flag, which will preserve the file contents, and seek to the end immediately when opening the file:
std::ofstream{ "Categories.txt", std::ios_base::out | std::ios_base::in | std::ios_base::ate };
The difference is that "app" should always append correctly, even when multiple streams are referring to the same file, while "in | ate" still allows you to move around and write in the file where you want/ necessary.
The clean answer for Boost Serialization would be
std::ofstream readFile("Categories.txt");
{
boost::archive::text_oarchive ar(readFile);
ar << vec;
ar << another_object;
ar << yet_another_object;
} // note destructs `ar`
readFile.close();
Note that you should make sure the archive is complete before you close the file. Sadly, that is handled by the aarchive destructor so you always get this clumsy life-time dance.
Of course, the file will close automatically when it leaves scope, so you could probably leave the readFile.close() out.
On combining multiple archives in a single stream: