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?
-
What "new object" did you add? Note that your `loadCereal` function is currently rewriting the file after loading. – paddy Apr 22 '21 at 01:03
-
How is it re-writing the file? That will be the reason it deletes everything everytime the program is ran. – Apr 22 '21 at 01:06
-
What do you expect happens when you construct a `std::ofstream` and then do `arr << vec;` ?? – paddy Apr 22 '21 at 01:34
2 Answers
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.

- 286
- 2
- 13
-
Boost serialization archives do not necessarily support multiple archives in one stream, though – sehe Apr 22 '21 at 12:38
-
Added a [small answer](https://stackoverflow.com/a/67213528/85371),. On combining multiple archives in a single stream: https://stackoverflow.com/questions/27422557/outputting-more-things-than-a-polymorphic-text-archive/27424381#27424381 +1 for the nice explanation of `std::basic_fstream` with `ios::openmode` Good explanations are rare. Hope to see you around on this site :) – sehe Apr 22 '21 at 12:52
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:

- 374,641
- 47
- 450
- 633
-
Thanks for your reply! This helps a lot. However, say if I run the program again and try to add something to the serialized file, it will delete the contents of whats previously in there. I don't really fully understand why its doing this. – Apr 22 '21 at 14:21
-
It does this because you're telling it to. That's what [Hernando's answer](https://stackoverflow.com/a/67210249/85371) atually nicely explains. – sehe Apr 22 '21 at 14:24
-
Yes, thank you. I've just tried it. If I put the contents of two text files into it, it now creates a new line. How would I extract the exact lines I need? – Apr 22 '21 at 14:32
-
By reading everything and using what you need. Boost archives are stream-based, not random access. If you need a database, use a database. If you need a random access archive, use something like a ZIP-file? – sehe Apr 22 '21 at 14:57