![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
Another way to obtain a TextStream object is to create a File object, then use its OpenAsTextStream method. Creating a File object is done like this, assuming that fs is a FileSystemObject: Dim f Set f = fs.GetFile(filename) For this method to work, the file specified by filename must exist (youll learn more about creating File objects in the next chapter). To create a new file, you would use CreateTextFile and then create the File object: fs.CreateTextFile filename Set f = fs.GetFile(filename) Once you have the File object, you can invoke the OpenAsTextStream method. The syntax is: f.OpenAsTextStream([iomode, [format]]) The iomode and format arguments are the same as described earlier for the CreateTextFile and OpenTextFile methods. Heres the code necessary to create a TextStream object associated with an existing file DEMO.TXT for reading using the OpenAsTextStream method: Dim fs, f, ts Set fs = CreateObject(Scripting.FileSystemObject) Set f = fs.GetFile(demo.txt) Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault) So far, we have seen how to create a TextStream object. Once you have done this, what next? The TextStream object has properties and methods that you use to write and read text. The properties are described in Table 13.4. In reading this table, be aware that TextStream object always has a current character position, also called the file pointer, that indicates where the next read or write operation will take place. Note that all of these properties are read-only. You use the TextStream objects methods to read and write data. These methods are described in Table 13.5.
Be aware that some of these methods are applicable only when the file has been opened for reading or writing. If you try to use a method that is inappropriate for the files mode, an error will occur. An FSO Text File Demonstration The program given here is a simple demonstration of using FSO to read and write a text file. The programs form, shown in Figure 13.2, contains one Text Box and a control array of three Command Buttons. Other than setting captions, the only property you need to change is to set the Text Boxs Multiline property to True. The projects code is presented in Listing 13.1. You can see that the name for the file is defined as a program constant; you could easily modify the program so it would prompt the user for a file name. In the LoadTextFromFile procedure, I have used error handling to deal with the possibility that the user will try to load the file before saving it. This would result in an error when the OpenTextFile method fails to find the file. By trapping the error, the program presents an informative message to the user rather than simply crashing.
Listing 13.1 The code in FSODEMO.FRM. Option Explicit Const FILENAME = FSODEMO.TXT Private Sub Command1_Click(Index As Integer) Select Case Index Case 0 Save Call SaveTextToFile Case 1 Load Call LoadTextFromFile Case 2 Quit End End Select End Sub Public Sub LoadTextFromFile() On Error GoTo ErrorHandler Dim fs, f, msg Set fs = CreateObject(Scripting.FileSystemObject) Set f = fs.OpenTextFile(FILENAME, ForReading) Text1.Text = f.ReadAll f.Close Exit Sub ErrorHandler: If Err = 53 Then msg = File Not Found Else msg = Error opening & FILENAME End If MsgBox (msg) End Sub Public Sub SaveTextToFile() Dim fs As New Scripting.FileSystemObject Dim f Set f = fs.CreateTextFile(FILENAME, True) f.write Text1.Text f.Close End Sub Using Random Access FilesA random access file is similar to a sequential access file in that both consist of records. Random file records must all be the same size, however, and the program differentiates one record from the next by its position in the file. No special character is used to separate records. For example, if a files records are each 100 bytes long, bytes 1 through 100 contain the first record (byte positions in a file always start at position 1), and record 4 is at positions 301 through 400. Random file records are numbered sequentially starting at 1, with a maximum of 2,147,483,647. (Not big enough for you? Too bad.) Each record is divided into one or more fields. The fields, too, are defined by their length. Each record in a random file contains the same field structurethe same number of fields and the same field sizes. Before you can create or use a random file, you must define its record 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. |