Introduction
Let's say you need to store and read an array of some structures to or from the hard drive in your application.
For instance: you may have a structure like this:
struct record {
char first_name[32];
char last_name[64];
record() { first_name[0] = 0; last_name[0] = 0; }
void set(const char* first, const char *last)
{
strncpy(first_name, first, sizeof(first_name)-1);
strncpy(last_name, last, sizeof(last_name)-1);
}
};
And you would like to persist an array of them.
Instead of using expensive (in many senses) classical database solution, you may write the following:
#include "tl_mm_file.h"
int main(int argc, char* argv[])
{
tool::table<record> tbl;
tbl.open("c:/test.tbl",true);
tbl[0].set("Andrew","Fedoniouk");
tbl[1].set("Allan","Doe");
tbl[2].set("Monica","Lester");
tbl.close();
tbl.open("c:/test.tbl");
for(unsigned int i = 0; i < tbl.total(); i++)
printf("%s %s\n",
tbl(i).first_name,
tbl(i).last_name );
tbl.close();
return 0;
}
You just need to include mm_file.h and mm_file.cpp in your project and you are done.
Pros
- Easy and fast. This method of accessing persistent data has nearly zero time overload for opening/closing datasets and eliminates need of intermediate buffers a.k.a. "current record".
- It provides direct access to the records in the table in a random manner.
- Compact and self-sufficient implementation.
Cons
- Only fixed size structures are supported.
- Access by key (hash-table, b-tree, etc.) should be implemented separately (if needed).