Thursday 8 January 2009

More on Random Access - Data Schemes

You have total freedom to derive your own schemes. If the blocks of data (records) are all the same length and stored one after another (like the chess positions) then you can easily calculate the position of record n (where n is 0..max number of records -1). Record n is at n* sizeof(record). If records vary in length (e.g. say you are storing lines in a text editor to disk) then you need to maintain a table of offsets. If the first record is 20 chars long and the 2nd one 45 then record 0 starts at 0, record 1 at 20 and record 2 at 65.

These days with the prevalence of cheap or free databases such as SQLite, MySql and SQL Server 2005 Express, there is less of a need to use random access on binary files. Databases are for another lesson though. In a sense, random access file records is an old fashioned technique but still useful. Serialization is probably more common.

I've used various data handling schemes based on random access files in the past before databases were commonplace. You might want to hold a game map as a file of class objects and fetch the object at location x,y. If the map is say 64 x 64 then the first 64 file records in row 0 hold locations (0,0) to (63,0), the next 64 hold (0,1) to (63,1) and so on. If the sizeof(map_location) is 20 then the whole file is 64*64*20 = 81,920 bytes long.

The example on the next page implements a simple file holding 1000 strings each 10 characters long. Because a fstream can only move the point where you read or write to a particular byte, you have to know where an individual record starts. If the records are the same length then it's a simple multiplication = record number * sizeof (record). If the records are different length then you have to maintain an index. Example 3 uses an index.

0 comments: