Some examples would be helpful, yes.
As to memory mapped files writing (storing) data, yes, it does that. There are some caveats. Technically, the file has to be at least the size as the data mapped. Put another way, it takes some effort to start with an empty file and expand it (more than it does to just write to a stream, but certainly something that can be done).
Once the file is large enough to store all of the data, however, memory mapping works to read from and write to the file automatically. Whatever is written to the RAM mapped to the file ends up being written to the file. It's as if you could connect a large array (as an example) to a file directly, without having to perform reading and writing using a file object or handle.
Let's say we're talking about this:
1 2 3 4 5 6 7
|
struct Data
{
unsigned int date, time, mil;
double recent, back, front;
unsigned int totals;
unsigned short green, red, hour, min, seconds;
}
| |
Note, it's missing a semicolon (a direct copy of your post).
I assume that since you've discussed loading this into a vector, what you have is basically this struct repeated (obviously with various data assigned) millions of times (I'm guessing about 45 million if these are 64 bit integers, perhaps 55 million or so if they're 32 bit integers - depends on the compiler even targeting a 64 bit application).
So, a file would be set to 3 Gbytes in size (accommodating the 40-60 million structs - it's a simple command), which would then be mapped as 3 Gbytes (assuming you have that RAM on the target machine and we're building a 64 bit application).
At that point, you would treat the address of the memory as a container, perhaps an array (it's nasty to use a std::vector in this situation). You might want the first 64 bit integer to contain the total number of entries (you could then re-use this on subsequent executions as a persistent container). There could be other "header" data as well if you need.
Then, just after that "header" data, you establish a pointer which is declared as a Data *, and use it as one large array of 40-60 million structures. Read/write to them at will, they'll be written to disk, read from disk as required.
That would be it. You need do nothing else. Well...you have to be sure you don't run past the end of the file, like any container, but when your application closes and this map object is closed, the file has the data you wrote as if it were one large array (and, that header I assume you'd use).
You might finish thinking it's a kind of magic. Files without writing to a file, just writing to a large RAM buffer cast as an array pointer.