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
Text In Sequential Files
One 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:
filenumThe number associated with the file when it was opened (in OUTPUT or APPEND mode).
listOne or more string expressions to be written to the file. Multiple expressions in list should be separated by a comma or semicolon. If list is omitted, Print # writes a blank line to the file.
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):
No comma or semicolonSubsequent output is placed on a new line.
SemicolonSubsequent output is placed on the same line immediately following the previous output.
CommaSubsequent output is placed on the same line at the next print zone.
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:
Without a comma or semicolon
Statements:
Print #1, Visual
Print #1, Basic
Written to file:
Visual
Basic
With a semicolon
Statements:
Print #1, Visual;
Print #1, Basic
Written to file:
VisualBasic
With a comma
Statements:
Print #1, Visual,
Print #1, Basic
Written to file:
Visual Basic
Next, lets take a look at the difference between using a comma or a semicolon as a separator between expressions:
With a semicolon
Statements:
Print #1, Visual;Basic
Written to file:
VisualBasic
With a comma
Statements:
Print #1, Visual,Basic
Written to file:
Visual Basic
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:
filenumThe number assigned to the file when it was opened (for INPUT).
varThe type String variable to receive the line of text.
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:
CreateTextFileCreates a new text file. If a file of the same name already exists, it is overwritten.
OpenTextFileOpens a text file for reading and/or writing. If the file already exists, new data is appended to existing data.
Figure 13.1 Use the Object Browser to view library information.
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:
filenameA string expression specifying the name, including path information, of the file.
overwriteTrue or False indicating whether an existing file will be overwritten. If this argument is omitted, the default is False. If overwrite is False and filename already exists, an error occurs. You can trap this error to permit the user to verify file overwrites.
UnicodeTrue to create a Unicode file, False (the default) for an ASCII file.
iomodeSet to the constants ForReading or ForAppending to read the file or write data to the end of the file. You cannot write to a file opened in ForReading mode.
createTrue or False specifying whether a new file will be created if filename does not exist. The default is False.
formatA tristate argument (one that can have three values) that determines the format of the file. TriStateTrue opens the file as Unicode, TriStateFalse (the default) opens the file as ASCII, and TriStateUseDefault uses the system default format setting.
TIP: What Is Unicode?
When dealing with text, computers use numbers to represent characters, punctuation marks, and so on. The two systems for this are ACSII (or ANSI) and Unicode. ASCII uses one byte per character, permitting only 256 different symbols to be represented. This is adequate for English, but cannot support many other languages. Unicode uses two bytes per character, permitting more than 65,000 different symbols. Which format you use depends on the specific requirements of your project.
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:
83 USTAWA o Państwowej Inspekcji Pracy [13 04 2007][Dz U 1Afganistan publiczna egzekucja pary kochanków (13 04 2009)Kapitel 13 04 eDeming do wykładu ZJ 2015 13,0413 04 Roboty mostowekomunikacja 13 04 201113 04WSM 04 13 pl(1)Hiren s BootCD 13 2 (Rebuild 20 04 2011) opis04 13egzamin próbny florysta 20 04 13 J Chabroswięcej podobnych podstron