Click here to Skip to main content
13,053,276 members (61,104 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 25 Aug 2003

DataStore - A template class for creating sequential access record files

, 15 Nov 2006
Rate this:
Please Sign up or sign in to vote.
A template class for creating sequential access record files


"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

For using 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.

struct Point 
  int x;
  int y;

DataStore ds("points.dat");

For writing an object to the file, call the AddRecord() member function. The new record will be appended to the end of the file.

Point p;
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, ModifyRecord() and DeleteRecord() functions take the record index as their arguments to perform operations on the file.


After file operations are over, you should call the DataStore object's 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.


This article, along with any associated source code and files, is licensed under The BSD License


About the Author

No Biography provided

You may also be interested in...


Comments and Discussions

GeneralTwo questions Pin
BenTsai28-Apr-05 1:17
memberBenTsai28-Apr-05 1:17 
GeneralRe: Two questions Pin
Vijay Mathew Pandyalakal28-Apr-05 17:41
memberVijay Mathew Pandyalakal28-Apr-05 17:41 
GeneralRe: Two questions Pin
BenTsai28-Apr-05 18:01
memberBenTsai28-Apr-05 18:01 
GeneralRe: Two questions Pin
Vijay Mathew Pandyalakal28-Apr-05 21:57
memberVijay Mathew Pandyalakal28-Apr-05 21:57 
QuestionShouldnt it be? Pin
Cabaleiro8-Oct-03 20:11
memberCabaleiro8-Oct-03 20:11 
GeneralArticle Question Pin
D N Harris8-Sep-03 6:48
memberD N Harris8-Sep-03 6:48 
GeneralSuggest further extention/encapsulation.. Pin
c++developer28-Aug-03 22:12
memberc++developer28-Aug-03 22:12 
GeneralSerialization Pin
srana27-Aug-03 2:50
membersrana27-Aug-03 2:50 
GeneralDatastore - storing a class instead of struct Pin
rwomack26-Aug-03 11:12
memberrwomack26-Aug-03 11:12 
GeneralRe: Datastore - storing a class instead of struct [modified] Pin
Vijay Mathew Pandyalakal27-Aug-03 4:45
memberVijay Mathew Pandyalakal27-Aug-03 4:45 
Generaltwo suggestions Pin
Stober26-Aug-03 8:21
memberStober26-Aug-03 8:21 
GeneralRe: two suggestions Pin
Vijay Mathew Pandyalakal27-Aug-03 4:23
memberVijay Mathew Pandyalakal27-Aug-03 4:23 
Questioniostream.h? Pin
DrPizza26-Aug-03 4:01
memberDrPizza26-Aug-03 4:01 
GeneralRe: iostream.h? Pin
Jim Crafton27-Aug-03 6:29
memberJim Crafton27-Aug-03 6:29 
GeneralRe: iostream.h? Pin
Anonymous28-Aug-03 3:52
sussAnonymous28-Aug-03 3:52 
Questionold c better than iostreams ? could you comment ? Pin
Jonathan de Halleux25-Aug-03 21:47
memberJonathan de Halleux25-Aug-03 21:47 
Answer[Message Deleted] Pin
Vijay Mathew Pandyalakal27-Aug-03 4:34
memberVijay Mathew Pandyalakal27-Aug-03 4:34 
GeneralRe: old c better than iostreams ? could you comment ? Pin
Jonathan de Halleux27-Aug-03 4:40
memberJonathan de Halleux27-Aug-03 4:40 
GeneralRe: old c better than iostreams ? could you comment ? Pin
akrc27-Aug-03 7:58
memberakrc27-Aug-03 7:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 15 Nov 2006
Article Copyright 2003 by AnOldGreenHorn
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid