576 579




Visual Basic 6 Black Book:File Handling And File Controls
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive 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 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } 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.



Wyszukiwarka

Podobne podstrony:
576 579
579 582
2000 10 Szkoła konstruktorówid!579
01 (576)
Dz U 1998 Nr 91 poz 576
579 Finanse Przedsiebiorstw co powinienem wiedziec zaoczni
579 (2)
fryzury; ;wlosy;dlugie,artykul,579
574 576
ONKYO KODY DO PILOTA 506,576,606,5100codes
Warunki techniczne Dz U 07 86 579

więcej podobnych podstron