For Starters...
I was recently helping someone with Random Files, and I realized as I googled along, that it took me some time to find any kind of information about random files, and how to go about working them. I had to pop in an old 3 1/2 inch floppy, and extract some ancient code that I had worked on in high school to get myself back up to speed. That said, it only seems right that the Daniweb community should have something to keep us brushed up on this.
What Are Random Files?
Random files, in the simplest possible explanation, are databases. That's a pretty bold statement, when you take into consideration access and SQL, but a random file is a file that holds records that are always the same. Don't get confused when I say that, because it would be silly to have a file with 10 records of the same person.... what I mean, is that all the type of information in the fields is the same. So, you might want to have a rolodex program, that keeps track of your contacts. You know that you will always have a string (sequence of characters) for the First and Last Name.... you will most likely have numbers for the zip code, and so forth. One of the Advantages to using random files, in place of a Flat File, is that you can work with 1 record at a time, without having to loop through the entire file. The Biggest DrawBack to using this (as opposed to a flat file) is the extreme loss of portability.
Understanding Random Files
The Key to understanding random files, is understanding that each record in the file is always the same size, no matter what. So, You have to reserve enough space for each field to ensure that you can fit the required data. This isn't a problem for things that never change, such as social security numbers (9 digits), Phone Numbers (7, or 10), Dates, etc, etc.... but for things like Names, Addresses, Comments, and Notes this can be a potential problem. You can't make it 5 characters long and input a name like "Comatose", because if the program doesn't flip out on you, then at best you'll get "Comat". The solution is to have an over-abundance of space.... like 20 characters for the first name. If there name is less than 20 characters, then when it gets written to the file, the rest of the characters are just spaces (place holders). Reasonably, I've never met anyone with a first name that is 20 letters long. The bad news about this, is that there is a waste of space in the file. If my Name is only 8 letters long, and we are saving that field with 20 letters, we have 12 bytes of nothingness just eating up space in the file.
It has to be that way though, because every record has to be the same size, no matter what. This makes it easy to calculate the number of records in the file, because a little division makes it real easy (if each record takes 5 bytes, and the size of the file is 25 bytes, then there are 5 records (25 / 5)).
User Defined types
Visual Basic Offers us a bit of freedom by letting us create our own data structures for the information to be stored in the random file. These User Defined Types allow us total control over how the records are setup, and what kind of data is stored in them. Just an example of a rolodex user defined type, might look like this:
Public Type MyContact
FirstName as string * 10 ' /* Allocate 10 Characters */
MiddleInitial as string * 1 ' /* 1 Character For Middle Initial */
LastName as string * 10
PhoneNumber as long
End Type
In The Strings (since strings can have a variable length of characters) we have to use the asterix in order to tell VB how big the string will be. The reason we have to do this, as mentioned above, is because each record has to be the same size.... that's how we know where they are in the file. We know where it starts, and we know how big it is, so we can figure out where all the other ones are too, right?
Wrapping It Up
Just Like Everything, Random Files have pro's and con's. Sometimes a relational database system simply isn't needed. "Never Use a Cannon To Kill A Fly" as often I've heard, so sometimes using an SQL type database is just over-kill. Next, We'll go over how to work with these files in code, and build a module to simplify using Random Files in the future.