13 05 R5LCUIOORANHLY4EVFEWI5AWPO5OEBKIBVO2O4Y




Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!:Working With Files
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 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 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




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 (you’ll 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. Here’s 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 object’s methods to read and write data. These methods are described in Table 13.5.
Table 13.4 Properties of the TextStream object.



Property
Description

AtEndOfLine
True, if the file pointer is at the end of a line; False otherwise. This property applies only to TextStream files that are open for reading; otherwise, an error occurs.

AtEndOfStream
True, if the file pointer is at the end of the file; False otherwise. This property applies only to TextStream files that are open for reading; otherwise, an error occurs.

Column
Returns the column number of the file pointer. The first character on a line is at column 1.

Line
Returns the current line number.




Table 13.5 Methods of the TextStream object.



Method
Description

Close
Closes the file associated with the TextStream object. You should always execute the Close method when you are done reading/writing the file.

Read(n)
Reads the next n characters from the file and returns the resulting string.

ReadAll
Reads the entire file and returns the resulting string.

ReadLine
Reads an entire line (up to, but not including, the newline character) from a TextStream file and returns the resulting string.

Skip(n)
Skips ahead (moves the file pointer) by n characters.

SkipLine
Skips to the beginning of the next line.

Write(s)
Writes the string s to the file. No extra or newline characters are added.

WriteBlankLines(n)
Writes n blank lines to the file.

WriteLine(s)
Writes the string s to the file, followed by a newline character.



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 file’s 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 program’s 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 Box’s Multiline property to True.
The project’s 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.

Figure 13.2  The FSO demonstration program.

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 Files
A 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 file’s 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 structure—the same number of fields and the same field sizes. Before you can create or use a random file, you must define its record structure.




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. Read EarthWeb's privacy statement.



Wyszukiwarka

Podobne podstrony:
13 05 NO 3 4 juda
29 04 & 06 05 & 13 05 2009, wykład, Wzrost, różnicowanie i starzenie się komórek Paul Esz
13 05 Nowicki?mage Buildup
13 05
P31 05 08 13[1]
13 F II wyklad 22 05 13
138 05 (13)
pętle 2 wym zad 1 14 05 13
zad2 6 05 13
pętle 2 wym zad 3 zmienione na ujemne 14 05 13
Badanie płytą 16 05 13 MC 20 ( rondo 1 w wa)

więcej podobnych podstron