![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
Text In Sequential FilesOne common use for sequential files is to store textsuch as a document or READ.ME filethat is not divided into fields. Each line, or record, in the file is simply treated as a line of text with no delimiters or other special characters. You can read and write text files using either traditional or FSO methods. Traditional Text File Access To write lines of text to a sequential file, use the Print # statement. Print # does not delimit fields with commas or enclose strings in double quotes. Rather, it writes data to the file with the exact same format as if the data had been displayed on the screen with the Print method. You saw Print # used in Chapter 11 in the Baby Editor project. The syntax for this statement is Print #filenum [, list] [,|;] with these arguments:
The optional comma or semicolon at the end of the Print # statement determines the location of subsequent output to the file (that is, the output of the next Print # statement):
Looking back to Chapter 9, you will see that the program outputs multiple lines to a file with a single Print # statementa useful feature. If the text that is being output (in Chapter 11, for example, the Text property of a Text Box control) contains CR-LF characters, they serve to break the output into multiple lines in the sequential file. It has the same effect as writing each line of the text to the file with a separate Print # statement. You can see that Print # is quite flexible: It has the ability to output part of a line of text (if terminated with a semicolon or comma) or multiple lines of text (if the text contains its own CR-LF characters). If neither of these conditions is met, Print # outputs one entire line of text to the file. Lets look at some other examples. The following illustrates what happens when a comma or semicolon appears at the end of the Print # statement:
Next, lets take a look at the difference between using a comma or a semicolon as a separator between expressions:
To read lines of text one at a time from a sequential file, use the Line Input # statement. Line Input # reads an entire line of text from the file without regard to field delimiters, assigning it to a string variable. The syntax is Line Input [#]filenum, var where the arguments are:
FSO Text File Access The FSO approach to file access is based upon something called a FileSystemObject. The first required step is to create an instance of this class: Dim fs Set fs = CreateObject(Scripting.FileSystemObject) You can also use the following syntax: Dim fs As New Scripting.FileSystemObject Note the required use of the Scripting qualifier, which identifies the library that the FileSystemObject class is defined in. You do not need to select this library, called the Microsoft Scripting Runtime, in the Visual Basic References dialog box to use the class, but doing so will give you access to the classes, methods, and properties in the Object Browser. The browser can be a useful source of information when you are programming. Remember, press F2, then select the desired library at the top left. Figure 13.1 shows information about the Scripting library displayed in the Object Browser. Once you have created an instance of the FileSystemObject class, the next step is to create a TextStream object, which is nothing more than a regular text file enclosed in an FSO wrapper. FileSystemObject has two methods for creating TextStream objects:
The syntax for these methods is similar. In these examples, assume that fs is a FileSystemObject and that ts has been declared as a type Variant or Object: Set ts = fs.CreateTextFile(filename[, overwrite[, unicode]]) Set ts = fs.OpenTextFile(filename[, iomode[, create[, format]]]) The arguments for these statements are as follows:
|
![]() |
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. |