0

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!!

Angelo
From italy

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Salem
0

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.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.