![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
File PositionEvery open binary file has its own file pointer. The file pointer is a numeric value that points at the position in the file where read and write operations will occur. Each position in a binary file corresponds to one byte of data. Therefore, a file that contains n bytes has positions numbered from 1 through n. To change the file pointer and determine its value, use the Seek statement and the Seek and Loc functionsthe same statements you use when working with random access files. With random access files, the file pointer points at records, not bytes. Visual Basic knows the ACCESS mode in which a given file was opened, and interprets Seek or Loc correctly when you use themas bytes for a binary file and records for a random file. The syntax of the Seek and Loc statements is Seek [#]filenum, newpos [pos =] Seek(filenum) [pos =] Loc(filenum) where filenum is the number associated with the file when it was opened in binary mode. The Seek statement sets the file pointer to newpos. The Seek function returns the current position of the file pointer (the next byte to be read or written). The Loc function returns the position of the last byte that was read or written. Unless you explicitly move the pointer, Loc is one less than Seek. Reading And Writing Binary File DataTo read data from a binary file, use the Get statement. To write data to a binary file, use the Put statement. These are the same statements used for random files; they are interpreted differently, depending on the mode of the file being read or written. The syntax is GET [#]filenum, [pos] , var PUT [#]filenum, [pos] , var with the following arguments:
When a binary mode file is first opened, the file pointer is at position one. Any Get or Put operation automatically increments the file pointer by the number of bytes transferred. To determine the length of a binary file, use the Lof function [length =] Lof(filenum) where filenum is the number associated with the file when it was opened in binary mode. The value returned by Lof is equal to the file-pointer position of the files last byte. A program can quickly read an entire binary file, using Lof and a loop. The following code shows how to use binary file access to make a copy of a file. It is assumed that ORIGINAL.DAT is an existing file, and that COPY.DAT does not exist. By using a one-character fixed-length string, the program reads and writes one byte at a time. While this code would certainly work, it is inefficient because of the byte-at-a-time approach: Dim ch As String * 1, count As Integer, f1 As Integer, F2 As Integer f1 = freefile Open ORIGINAL.DAT For BINARY AS #f1 f2 = freefile Open COPY.DAT For BINARY AS #f2 For count = 1 TO Lof(1) Get #f1, , ch Put #f2, , ch Next count Close #f1, #f2 The EOF functiondescribed earlier in the chaptercan be used to detect the end of a binary file. For example, the loop in the previous code could also have been written as follows: While Not Eof(f1) Get #f1, , ch Put #f2, , ch Wend The flexibility of binary files makes them an excellent choice for storing program data, particularly when the data format does not lend itself to a random access or sequential access file. Remember that the unstructured nature of binary files makes it necessary for our program to keep track of the data-storage format. For example, a program that uses an array of 1,000 double-precision values could store the entire array in a binary file as follows: Dim data(999) As Double, count As Integer Open ARRAY.DAT For BINARY As #f1 For count = 0 To 999 Put #f1, , data(count) Next count Close #1 The data could later be retrieved from the disk file back into the array, as follows: Open ARRAY.DAT For BINARY As #f1 For count = 0 To 999 Get #f1, , data(count) Next count Close #1 Although nothing prevents us from retrieving the data into an array of type Integer or any other Basic data type, the results would be meaningless. Here are a few hints for working with binary files:
Which File Type Should You Use?If you are a bit confused by Visual Basics different file-access modes, you are not alone. Its not always clear which type of file is best for a particular application. The following guidelines are designed to help you choose:
These are just guidelines, not meant to be interpreted as hard-and-fast rules. Its important to understand that a files access mode does not affect the physical file on disk, but only the way a program reads and writes the file data. In other words, any fileregardless of which mode was used to create itis simply a sequence of bytes stored on the disk. For example, you could create a file using random access mode, then later open and read it in binary access mode. As your programming skills develop, you will sometimes find that mixing file access modes is the preferred approach to your data-storage needs. Until you are quite familiar with all three file access modes, however, I suggest you follow the previous guidelines and limit yourself to one access mode per 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. |