Reading and writing raw data from/to binary files
Some of the data programs you work with must be persisted to disk files in various ways, including storing data in a database or to flat files, either as text or binary data. This recipe, and the next one, are focused on persisting and loading both raw data and objects from and to binary files. In this context, raw data means unstructured data, and, in this recipe, we will consider writing and reading the content of a buffer (that is, a contiguous sequence of memory) that can either be an array, an
std::vector, or an
For this recipe, you should be familiar with the standard stream I/O library, although some explanations, to the extent that is required to understand this recipe, are provided next. You should also be familiar with the differences between binary and text files.
In this recipe, we will use the
ifstream classes, which are available in the namespace