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 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Now that we’ve created a TextStream, we can write to it, as we’ll see later in this chapter.

Opening A TextStream

To open a TextStream, you use the FileSystemObject’s OpenTextFile method:

FileSystemObject.OpenTextFile(  filename[,  iomode[, create[, format]]])

Here are what the arguments to OpenTextFile mean:

  filename—The file to open.
  iomode—Indicates input/output mode. Can be one of two constants, either ForReading or ForAppending.
  create—Boolean value that indicates whether a new file can be created if the specified file doesn’t exist. The value is True if a new file is created; False if it isn’t created. The default is False.
  format—One of three values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.

Here’s an example where we open a TextStream object corresponding to a file named file.txt:

Private Sub Command2_Click()
   Dim FileSystemObject, TextStream As Object

   Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
   Set TextStream = FileSystemObject.OpenTextFile("c:\file.txt")
End Sub

After you’ve opened a TextStream object, you can read from it, as we’ll see later in this chapter.

Writing To A TextStream

To write to a TextStream object, you use one of these methods:

Write( string)
WriteLine([ string])

Here’s an example where we create a file named file.txt and write a string, “Here is some text!” to that file. First, we create a new TextStream:

Private Sub Command1_Click()
   Dim FileSystemObject, TextStream As Object

   Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
   Set TextStream = FileSystemObject.CreateTextFile("c:\file.txt", True)
...

Then we write our line of text to the file and close that file:

Private Sub Command1_Click()
   Dim FileSystemObject, TextStream As Object

   Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
   Set TextStream = FileSystemObject.CreateTextFile("c:\file.txt", True)
   TextStream.WriteLine ("Here is some text!")
   TextStream.Close
End Sub

Reading From A TextStream

To read from a TextStream object, you use one of these methods; note that the Read method lets you specify how many characters to read:

Read( numbercharacters)
ReadAll
ReadLine

Each of these methods returns the text read. Let’s see an example. In this case, we’ll open a file, file.txt, and read one line from it, displaying that line in a text box. First, we create a TextStream object for that file:

Private Sub Command1_Click()
   Dim FileSystemObject, TextStream As Object

   Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
   Set TextStream = FileSystemObject.OpenTextFile("c:\file.txt")
...

Next, we use the ReadLine method to read a line from the file and display it in a text box, Text1, and close the TextStream:

Private Sub Command1_Click()
   Dim FileSystemObject, TextStream As Object

   Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
   Set TextStream = FileSystemObject.OpenTextFile("c:\file.txt")

   Text1.Text = TextStream.ReadLine
   TextStream.Close
End Sub

Closing A TextStream

When you’re finished working with a TextStream object, you close it using the Close method. In the following example, we write to a file, file.txt, using a TextStream object and then close that TextStream (and therefore the file) using Close (this method takes no arguments):

Private Sub Command1_Click()
   Dim FileSystemObject, TextStream As Object
   Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
   Set TextStream = FileSystemObject.CreateTextFile("c:\file.txt", True)

   TextStream.WriteLine ("Here is some text!")
   TextStream.Close
End Sub


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.