- Introduction
- File Functions in Visual Basic
- Working with Text Files
- Random Files--Creating Your Own File Format
- INI Files
- From Here
Random FilesCreating Your Own File Format
Sequential files do not have any structure. As a matter of fact, the structure is defined by the code that reads the file rather than in the file itself. As you learned in the previous section, this feature is advantageous from a readability standpoint, but limits the functions that a programmer can use to search through the file. For example, Visual Basic does not contain a function to jump to a specific record in a sequential file because Visual Basic does not know the file's structure. One way to move some structure into the file itself is to store user-defined record types rather than just string data. You can then open them in Random mode and are not limited to sequential access.
Creating a Record Type
Custom record types are really just user-defined data types. You create user-defined data types by using the Type statement. Type declarations are entered in the general declarations section of a Code window. The following code uses the Type statement to declare an Employee record type:
Private Type Employee EmpID As Integer LName As String * 30 Fname As String * 20 Title As String * 20 End Type
The custom record type can then be accessed from code by using dot notation, as in the following example:
Dim emp1 As Employee Emp1.Fname= "Joe" Emp1.Lname= "Smith" Emp1.Title= "Chicken Plucker" Emp1.EmpID = 12345
Notice that accessing fields in a custom data type is similar to accessing an object's properties, as discussed in Chapter 4, "Using Visual Basic's Default Controls."
Opening a Random Access File
The main difference between opening a random access file and a sequential file is that you must specify the record length in the Open statement. The record length for a user-defined data type can be obtained by using the Len statement on a variable of that type, as in the following example:
Dim emp1 As Employee Open "D:\EMPINFO.DAT" for Random As #1 Len = Len(emp1)
The preceding line of code opens the file EMPINFO.DAT for random access. The Len= part of the Open statement tells Visual Basic that subsequent reads and writes to the file will assume that the file contains only records the length of emp1.
Adding Records with Put
After you open a file for random access, use the Put statement to store records in the file. The syntax of the Put statement is as follows:
Put filenumber,[recnumber],variablename
The following code uses the Put statement in a For loop to write five records of type Employee to file #1:
For i = 1 To 5 emp1.LName = InputBox$("Enter Last Name") emp1.Fname = InputBox$("Enter First Name") emp1.Title = InputBox$("Enter Title") emp1.EmpID = i Put #1, , emp1 Next I
Notice the omission of the recnumber parameter, which specifies the location in the file where the new record should be written. This approach is useful for altering existing records in a file. If you do not specify this parameter, the record is written to the current file pointer location.
Retrieving Records with Get
To retrieve records from a random file, use the Get statement. The Get statement can be used to read information back into your record type, as in the following example that reads record number 4 in the file into the variable emp1 and then displays the Title field:
Get #1, 4, emp1 MsgBox "Employee title is " & emp1.Title
The syntax of the Get statement is similar to Put:
Get filenumber,[recnumber],variablename
If you omit the recnumber parameter, Get behaves like the sequential Line Input statement; that is, each subsequent Get reads the next record.
Random Access with Seek
To move from record to record, use the Seek statement. The Seek statement has two parameters: the file number and record number, as shown here:
Seek #1,3
The preceding line of code causes the next Put or Get to access record number 3.