"DataStore" is a template class that wraps C file handling routines in a straightforward manner. This can be used in place of C++ iostream classes for easy serialization of C++ objects.
To understand the code presented here, you should have a basic knowledge of OOPs and C++ templates.
Using the code
DataStore, first include datastore.h using the
#include preprocessor directive. The template argument to
DataStore must be the name of a structure or class, the objects of which "
DataStore" will read and write on the file. The constructor of
DataStore takes the disk file name as its argument. This file must be opened using the
Open() function before any read/write operations can take place. The file will be created, if it does not already exist.
For writing an object to the file, call the
AddRecord() member function. The new record will be appended to the end of the file.
p.x = 10;
p.y = 30;
DataStore searches records based on its position in the file. For this, you have to pass the position of the record to the
FindRecord() member function. The following code reads the first record from the file:
Point pf = ds.FindRecord(0);
To search for a particular record based on some criteria, you should first get the total number of records in the file and then loop through the entire file until you find the record.
unsigned long recs = ds.GetRecordCount();
bool found = false;
for (unsigned long i=0;i
In the same way,
DeleteRecord() functions take the record index as their arguments to perform operations on the file.
After file operations are over, you should call the
Close() function to release the file handle.
The demo project creates a new data file, and inserts 100000 records into it. It then permits you to add new records and perform other file operations. (The add operation is performed automatically.) As the number of records grow, you will find that searching for newer records will become slower. This problem can be solved by adding indexing support by using some libraries like BDB. But as long as your application need not store millions of records, you can just be happy with the
DataStore class as it is.