Click here to Skip to main content
Licence BSD
First Posted 25 Aug 2003
Views 64,126
Bookmarked 23 times

DataStore - A template class for creating sequential access record files

By | 15 Nov 2006 | Article
A template class for creating sequential access record files
 
Part of The SQL Zone sponsored by
See Also

Introduction

"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.

Background

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");
ds.Open();

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;
ds.AddRecord(p);

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.

ds.ModifyRecord(new_point,1);
ds.DeleteRecord(0);

After file operations are over, you should call the DataStore object's Close() function to release the file handle.

ds.Close();

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.

License

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

About the Author

AnOldGreenHorn



India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralTwo questions PinmemberBenTsai1:17 28 Apr '05  
GeneralRe: Two questions PinmemberVijay Mathew Pandyalakal17:41 28 Apr '05  
GeneralRe: Two questions PinmemberBenTsai18:01 28 Apr '05  
GeneralRe: Two questions PinmemberVijay Mathew Pandyalakal21:57 28 Apr '05  
QuestionShouldnt it be? PinmemberCabaleiro20:11 8 Oct '03  
GeneralArticle Question PinmemberD N Harris6:48 8 Sep '03  
GeneralSuggest further extention/encapsulation.. Pinmemberc++developer22:12 28 Aug '03  
GeneralSerialization Pinmembersrana2:50 27 Aug '03  
GeneralDatastore - storing a class instead of struct Pinmemberrwomack11:12 26 Aug '03  
GeneralRe: Datastore - storing a class instead of struct [modified] PinmemberVijay Mathew Pandyalakal4:45 27 Aug '03  
Generaltwo suggestions PinmemberStober8:21 26 Aug '03  
GeneralRe: two suggestions PinmemberVijay Mathew Pandyalakal4:23 27 Aug '03  
Questioniostream.h? PinmemberDrPizza4:01 26 Aug '03  
GeneralRe: iostream.h? PinmemberJim Crafton6:29 27 Aug '03  
GeneralRe: iostream.h? PinsussAnonymous3:52 28 Aug '03  
Questionold c better than iostreams ? could you comment ? PinmemberJonathan de Halleux21:47 25 Aug '03  
Answer[Message Deleted] PinmemberVijay Mathew Pandyalakal4:34 27 Aug '03  
GeneralRe: old c better than iostreams ? could you comment ? PinmemberJonathan de Halleux4:40 27 Aug '03  
GeneralRe: old c better than iostreams ? could you comment ? Pinmemberakrc7:58 27 Aug '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.5.120517.1 | Last Updated 15 Nov 2006
Article Copyright 2003 by AnOldGreenHorn
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid