4.3. Organizing a stream file into records

Stream files are not record-based, but rather are just a collection of bytes. However, when you're working with records in your favorite physical file, what are they? Nothing more than a fixed-length bunch of bytes, right?

Theoretically, if we wrote fixed-length chunks of data to a stream file, and called those chunks "records", then we could use lseek() to jump to the start of each record, and read it just like a non-keyed physical file!

But, why would you ever do that? After all, the DB2/400 physical files are much more efficient, aren't they? Well, yes. But, let's say our file was going to be read directly by a PC program... aha! The PC program probably doesn't understand how to access the physical file, but it sure knows how to read a stream file!

Calculating the offset where a record in a stream file starts should be pretty easy. If we know, for example, that a record is 68 bytes long, and we want to jump to the 10th record in a file, all we have to do is multiply, right? Well, not exactly. If the 10th record is at position 680, then that would mean that the first record would be at 1 x 68, or position 68. But, actually, the first record should be at offset 0, since that's the start of the file.

So, the formula for finding the offset for a start of a record is always: Offset = (RecordNo - 1) x RecordLength