![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
Opening FilesA 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:
File NumbersEach 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.
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. Heres 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 FilesA file should be closed when the program is finished using it. Closing a file flushes the files 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 files 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 FilesAs 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 anotherfrom writing to reading, for exampleyou must close the file and reopen it in the new mode. Fields In Sequential FilesYou 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 authors name, the title, and an index number that specifies the books location on your shelf. If you create a sequential file for this data, it will have the following structure:
|
![]() |
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. |