|
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
Lets say that the first entry in the file is The Aspern Papers by Henry James, index number 12. The record for that entry will be stored in the file, as shown here: Henry James,The Aspern Papers,12 If the second entry is for Dostoyevskys Crime and Punishment, index number 123, it will be stored as follows: Dostoyevsky,Crime and Punishment,123 Each record will be stored on its own line. If you opened the file in a text editor, such as the Visual Basic editor or the Windows Notepad, you would see the records just as they are shown here. From these examples, you can see that a sequential file uses commas and double quotation marks to delimit the fields. Fields are separated by commas, and text data is enclosed in quotation marks. Use the Write # statement to write a single record of field-delimited data to a sequential file. Its syntax is Write #filenum [, list] with these arguments:
Here are some examples. The following line of code would write one record to the sequential file of book information: Write #filenum, Henry James,The Aspern Papers,12 Of course, in a real program, you would use variables in the Write # statement: book.name = Henry James book.title = The Aspern Papers book.index = 12 Write #filenum, book.name, book.title, book.index Each Write # statement writes one record to the file, automatically inserting any necessary comma and quotation mark delimiters. Double quotation marks cannot be included in data written with the Write # statement. To read field-delimited data from a sequential file, use the Input # statement. Input # reads one or more fields from the input file and assigns them to program variables. Its syntax is Input #filenum, list with the arguments:
Input # reads one field from the file for each variable in its argument list, assigning the fields to the variables in order. Remember that sequential files must be read sequentially, starting with the first field in the first record and proceeding from that point. Heres a brief example: FileNum = FreeFile Open BOOKLIST.DAT For Output As #FileNum Write #FileNum, Henry James,The Aspern Papers,12 Write #FileNum, Dostoyevsky,Crime and Punishment,123 Close #FileNum Then, in another part of the program: Dim A As String, B As String, C As String, D As String Dim x As Integer, y As Integer FileNum = FreeFile Open BOOKLIST.DAT For INPUT As #FileNum Input #FileNum, A, B, x Input #FileNum, C, D, y Close #FileNum After this code executes, A = Henry James, B = The Aspern Papers, x = 12, C = Dostoyevsky, D = Crime and Punishment, and y = 123. The Input # statement reads data from the file on a field-by-field basis, assigning fields read from the file in the order in which variables appear in the Input # statements argument list. If you replaced the two Input # statements in the previous example with the single statement Input #FileNum, A, B, x, C, D, y or with the multiple statements Input #FileNum, A, B Input #FileNum, x, C Input #FileNum, D, y the results would be exactly the same. The way that Input # breaks file data into fields depends on the type of variable in the Input # statements argument list. If Input # is reading data into a string variable, the end of a field is marked by one of the following:
If Input # is reading data into a numeric variable, the end of the field is marked by:
When you are using a sequential file to store data, the program is responsible for keeping Write # and Input # statements synchronized in terms of the type, order, and number of fields in each record. In other words, data items must be read from the file in the same order they were written to it. If this synchronicity is disrupted, two kinds of problems can result:
One common use for sequential access files is to store an array of variable-length strings. This code fragment demonstrates how to write the array data to a disk file: Dim notes(100) As String, count As Integer, FileNum As Integer ... Code here puts data into the notes array. ... FileNum = FreeFile Open NOTES.TXT For OUTPUT As #FileNum For count = 0 TO 100 WRITE #FileNum, notes(count) Next count Close #FileNum The following code retrieves the data from the disk file and places it in a string array: Open NOTES.TXT For INPUT As #FileNum For count = 0 To 100 Input #FileNum, notes(count) Next count Close #FileNum You can also use a sequential file to store arrays of numbers, but as youll see later in the chapter, another file access mode is actually better suited for this task. Detecting The End Of The FileWhen your program is reading data from a sequential file, the EOF function enables you to detect when the program has reached the end of the file. The syntax is EOF(filenum) where filenum is the number associated with an open file. EOF returns True if the last record in the file has been read and False if it has not yet been read. Trying to read past the end of a sequential file will generate an error. The following code shows how to use EOF in a loop to read an entire sequential fileone line at a timeinto an array: Dim info(1000) As String Dim count As Integer ... Open MYFILE.DAT For Input As #filenum count = 0 While Not EOF(filenum) Line Input #filenum, info(count) count = count + 1 Wend
|
![]() |
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. |