Basics: A theory...
A long way
After having discussed a bit about streams, it is quite obvious that using them is a good affair. But how making such a file gathering these different streams? What we have to do is to create a reuseable component that occupies our data, so we can give it data, retrieve and save it to a file during runtime. At first, I want to show you a very simple way of storing information.
Variant A: gathering data in multiple streams
If your application using our fictional component has some data in form of a stream, we might pass it to that one. What must happen to store it? Well, it creates an own TMemoryStream that will exist as long the application runs. This stream will get the application's data. When the program has new data for us, another stream is created and retrieves the buffer, and so on. When needing the data, we just have to query the contents of one special stream. How to manage those streams exactly we will work out two pages later.
What I have left out is how to save all streams into a single file. Here is a solution: we might create one big "master stream", that reads one of our small streams after the next. At last, we have to add a table of contents (toc), where our component writes down where which stream begins and ends - then we just have to save this large stream to file using the ordinary SaveToFile() method. Later on, we can reopen the file, the component has to read out the table of contents, so that the application can demand information in form of a single stream that is extracted from the large one using the notations in the toc.
Variant B: gathering data in a single stream
This variant is that one we will discuss throughout the next pages and chapters. There we will directly manage and manipulate the data in one large chunk contained in a large TFileStream, TMemoryStream is only used temporary to put and retrieve data from that big one. The filestream can point to the saved file itself or to a backup on that we work, remind that modifications to a filestream are done directly to the file. We will use a backup, so changes are done on a temporary base and the original file can be saved if the user of your app is shure to save the modifications done. On the next page, you can see in detail how our component will work and how such a file is build up.