Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Opening Files

A program must open a disk file before it can read or write file data. Files are opened with the Open statement, which has the syntax

Open filename [For mode] [Access access] [lock] As [#]filenum [LEN=reclen]

with these arguments:

  filename—A string expression specifying the name of the file to be opened. If filename does not include a drive and path specifier, the file is opened in the current folder.
  mode—A keyword that specifies the file access mode, as shown in Table 13.1.
  access—An optional keyword that specifies the operations permitted on the open file, as shown in Table 13.2.
  lock—An optional keyword that controls access to a file by other processes. Valid values for lock are shown in Table 13.3.
  filenum—An integer expression with a value between 1 and 511. See the next section for more information on file numbers.
  reclen—An integer expression that specifies the record length (in bytes) for random access files, and the buffer size for sequential access files (maximum = 32,767; default = 128 for both). The buffer is an internal memory area used by Visual Basic programs to store data temporarily before it is written to disk. A larger buffer size can sometimes speed disk operations, but it also decreases memory available for other purposes. Note that reclen is not applicable to binary mode files.

File Numbers

Each file opened by a Visual Basic program has a unique file number associated with it. This number is specified by the filenum argument that you pass to the Open statement. After the file is open, the program uses the file number to refer to the file when reading and writing the file and when closing it. The file number must be within the range 1 through 511, and only one open file can be associated with a given number at a time. When the file is later closed, the file number is free to be used with another file.

Table 13.1 Mode types.

Mode File Access
APPEND Sequential access; if filename already exists, new data is added at the end of existing data in the file. If filename does not exist, it is created.
BINARY Binary access; reading and writing. If filename does not exist, it is created.
INPUT Sequential access; the file is opened for reading. If filename does not exist, an error occurs.
OUTPUT Sequential access; the file is opened for writing. If filename exists, it is deleted and a new file created. If filename does not exist, it is created.
RANDOM Random access; the file is opened for reading and writing. If filename does not exist, it is created. This is the default if the mode argument is omitted.


Table 13.2 Access types.

Access Result
READ File opened for reading only.
WRITE File opened for writing only.
READ WRITE File opened for reading and writing. This mode is valid only for random mode files, binary mode files, and sequential mode files opened for APPEND.


Table 13.3 Lock types.

Lock Effect
SHARED Any process may read or write the file.
LOCK READ No other process may read the file. A program can open a file in LOCK READ mode only if no other process has READ access to the file.
LOCK WRITE No other process may write to the file. A program can open a file in LOCK WRITE mode only if no other process has WRITE access to the file.
LOCK READ WRITE No other process may read or write the file. A program can open a file in LOCK READ WRITE mode only if no other process has READ or WRITE access to the file and a LOCK READ or LOCK WRITE is not already in place.

In a multitasking environment, such as Windows, other programs may have opened files that have used some of the available file numbers. Before opening a file, therefore, use the FreeFile function to obtain the lowest unused file number. Here’s how to use FreeFile:

Dim filenum As Integer
...
filenum = FreeFile
OPEN “c:\documents\sales.lst” FOR RANDOM AS filenum

When called with no argument or an argument of 0, FreeFile returns an unused file number within the range 1 through 255. An argument of 1 results in a number within the range 256 through 511:

filenum = FreeFile       ‘ 1-255
filenum = FreeFile(0)        ‘ 1-255
filenum = FreeFile(1)        ‘ 256-511

Closing Files

A file should be closed when the program is finished using it. Closing a file flushes the file’s buffer (a temporary storage area in memory), ensuring that all data has been physically written to disk. Closing a file also frees up the file number that was associated with the file, allowing it to be used for another file and freeing the memory that was assigned to the file’s buffer. To close a specific file or files, execute the Close statement with the file numbers as arguments. To close all open files, execute Close with no arguments:

Close #filenum
Close #f1, #f2, #f3
Close

All open files are automatically closed when a Visual Basic program terminates. Even so, develop the habit of closing individual files as soon as the program is finished with the file. This technique avoids the possibility of data loss in the event of a system crash or power failure.

Using Sequential Files

As I explained earlier, a sequential file stores data in a series of records where each record is a line of text with a carriage return-line feed (CR-LF) combination at the end. You can use a sequential file to store data in two ways: Each record is a single line of text; or each record is a group of fields separated by delimiters. This section covers both methods. Remember that a sequential file can be opened for only one type of operation at a time: to read data from the file (mode = INPUT), to write data to a new file (mode = OUTPUT), or to write data to the end of an existing file (mode = APPEND). To switch from one operation to another—from writing to reading, for example—you must close the file and reopen it in the new mode.

Fields In Sequential Files

You can use fields in a sequential file when the information to be stored consists of discrete units that all have the same structure. For instance, imagine you are writing a program to keep a catalog of your books. For each book, you want to store the author’s name, the title, and an index number that specifies the book’s location on your shelf. If you create a sequential file for this data, it will have the following structure:

  The file will contain one record for each book.
  Each record will contain three fields: one for author, one for title, and one for index number.


Previous Table of Contents Next


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.