I'm just writing a reading for the time a binary file where I stored records of my objects.

It seems woorks very well.

Now I'm working for random access at position "X":

ifstream myFile(fileName,ios::in); 
	myFile.seekg( (X*sizeof( object )) , ios::beg);

Should be ok.

Before I start to write code, I'm thinking my records contain dynamic array with diferrent sizes.

For example:
my record is an object to handle a tennis' game.
I've an array contains players' reference; This array can have a size of 2 or 4.

For access file rightly, can i mantein a dynamic array ot it's better to write an array fixed at 4 length ??

Excuse my bad english.
My english is only a little better than my c++ programming skill!!

From italy

Recommended Answers

All 2 Replies

Your english is good! If you want to do random access using seek(X*sizeof(obj)), every 'obj' must be stored in the same sized chunk.

If you use different sized objects, then you will either have to store a record allocation table somewhere in the file; or read the file sequentially until you find the right record; or put markers/tokens in the file so that you can semi-random/sequentially access the desired record.

If you've got variable types, then write out some kind of 'id' first, so that you know what kind of object is next in the file.
Similarly for variable lengths, write out the length, then the data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.