![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
Defining Random File Record StructureThe structure of a random files records is defined in the same manner as the user-defined data type created with the Type...End Type statements covered in Chapter 4. The first step is to create a user-defined data type that has the structure you want to use for the random file. This method works well, because almost all programs that use random files require the same user-defined data type to manipulate the data in the program. Continuing our earlier example of a filing system for books, you could write: TYPE Book_Data Author As String * 20 Title As String * 25 Index As Integer END TYPE DIM Books(100) As Book_Data Now you have a user-defined type that will manipulate the book data in the program, as well as define the record structure for the random access file. When opening a file for random access, one of the required parameters is the length of the files records (you saw this earlier in the chapter in the discussion of the Open statement). This task is easily accomplished using the Len function, which returns the length, in bytes, of a data type. The Open statement would read: filenum = FreeFile Open BOOKFILE.DAT FOR RANDOM AS #filenum Len = Len(Books(1)) Note that you passed the Len function one array element, not the entire array. This statement opens the random file with a record length equal to the size of the user-defined data structure. Reading And Writing Random FilesTo write data to a random file, use the Put statement. The syntax is Put [#]filenum,[recnum],usertype with the arguments as follows:
Continuing with the book-filing example, to write data to the first record in the file, you would write: Books(1).Author = Henry James Books(1).Title = The Aspern Papers Books(1).Index = 12 Put #FileNum, 1, Books(1) When first creating a random access file, you can start writing data at record 1, increasing the record number by 1 for each successive Put statement. You could also omit the recnum argument in the Put statement, and let Visual Basic automatically keep track of record numbers. If you are adding records to an existing file, however, you must control recnum to ensure that the data is placed where you want it. If you write new data to a record number that is already in use in the file, the new data will replace the old data. If you dont want to overwrite existing data, you must add new records at the end of the file. In other words, for a file that already has n records, start adding new records at record n + 1. How do you determine the number of records in a file? You use the Lof function, which returns the file length, or size, in bytes. Its syntax is Lof(filenum) where filenum is the number associated with the open file whose length you want to know. What is the benefit of knowing the files total size? You already know that the Len function returns the size of each record in bytes. It follows that the total bytes in a file divided by the bytes per record will give us the number of records in the file. Therefore, you would write: nextrec = (Lof(1) \ Len(usertype)) + 1 Put #FileNum, nextrec, usertype To obtain the number of the next record to be read or written, use the Seek function. Similarly, the Loc function returns the number of the last record read or written. To read data from a random access file, use the Get statement. Data is read from a specified record in the file and placed in a user-defined structure of the type used to create the file. The syntax of the Get statement is Get [#]filenum,[recnum],usertype with the arguments:
Again using the book-filing example, to read the first record of a random file, you would write: GET #FileNum, 1, Books(1) To read all records from a random file, use a loop that starts with the first record and reads sequential records until the end of the file is reached, placing them in the elements of an array of the appropriate user-defined type. To detect the end of file, use the EOF function, as shown earlier in this chapter for sequential files. You can also calculate the number of the last record in the file by dividing the file length by the record length, as shown previously. Visual Basic maintains a file pointer for each open random access file. This pointer specifies the record number that will be read or written by the Get or Put statement if the recnum argument is omitted. When a random access file is first openedwhether its a new file or an existing filethe file pointer initially points at record 1. As you read and write random file data, the file pointer is maintained as follows:
To determine the current file pointer position, you can use the Seek and Loc functions. Seek returns the position of the file pointer (the next record to be read or written). Loc returns the position of the last record that was read or written. The syntax is Seek(filenum) Loc(filenum) where filenum is the number associated with an open random access file. The value returned by Loc is one less than the value returned by Seek. When a file has just been opened, Loc returns zero and Seek returns one. Using Binary FilesA binary file stores data as an unformatted sequence of bytes. No record/field structure exists in the file (unless you impose one in code). Binary files provide a great deal of flexibility in storing data, but their unstructured nature forces the programmer to keep track of what is stored in the file. That necessitates keeping track of where you are in the file.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |