Here is the situation: A c++ program is endlessly generating data in a regular fashion. The data needs to be stored in persistent storage very quickly so it does not impede the computing time. It is not possible to know the amount of data that will be stored in advance. After reading this and this posts, I end up following this naive strategy:
- Creating one
std::ofstream ofs
- Opening a new file
ofs.open("path/file", std::ofstream::out | std::ofstream::app)
- Adding std::string using the operator
<<
- Closing the file has terminated
ofs.close()
Nevertheless, I am still confused about the following:
- Since the data will only be read afterwards, is it possible to use a binary (
ios::binary
) file storage? Would that be faster? - I have understood that flushing should be done automatically by
std::ofstream
, I am safe to use it as such? Is there any impact on memory I should be aware of? Do I have to optimize thestd::ofstream
in some ways (changing its size?)? - Should I be concerned about the file getting bigger and bigger? Should I close it at some point and open a new one?
- Does using
std::string
have some drawbacks? Is there some hidden conversions that could be avoided? - Is using
std::ofstream::write()
more advantageous?
Thanks for your help.