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
Lets say that the first entry in the file is The Aspern Papers by Henry James, index number 12. The record for that entry will be stored in the file, as shown here:
Henry James,The Aspern Papers,12
If the second entry is for Dostoyevskys Crime and Punishment, index number 123, it will be stored as follows:
Dostoyevsky,Crime and Punishment,123
Each record will be stored on its own line. If you opened the file in a text editor, such as the Visual Basic editor or the Windows Notepad, you would see the records just as they are shown here. From these examples, you can see that a sequential file uses commas and double quotation marks to delimit the fields. Fields are separated by commas, and text data is enclosed in quotation marks.
Use the Write # statement to write a single record of field-delimited data to a sequential file. Its syntax is
Write #filenum [, list]
with these arguments:
filenumThe number associated with the file when it was opened in OUTPUT or APPEND mode.
listA list of one or more Basic expressions to be written to the file. If list contains more than one item, separate them by commas. If list is omitted from the Write # statement, an empty record (a blank line) is written to the file.
Here are some examples. The following line of code would write one record to the sequential file of book information:
Write #filenum, Henry James,The Aspern Papers,12
Of course, in a real program, you would use variables in the Write # statement:
book.name = Henry James
book.title = The Aspern Papers
book.index = 12
Write #filenum, book.name, book.title, book.index
Each Write # statement writes one record to the file, automatically inserting any necessary comma and quotation mark delimiters. Double quotation marks cannot be included in data written with the Write # statement.
To read field-delimited data from a sequential file, use the Input # statement. Input # reads one or more fields from the input file and assigns them to program variables. Its syntax is
Input #filenum, list
with the arguments:
filenumThe number associated with the file when it was opened (in INPUT mode).
listA list of one or more program variables to be assigned data that will be read from the file. If list contains more than one variable name, separate them by commas.
Input # reads one field from the file for each variable in its argument list, assigning the fields to the variables in order. Remember that sequential files must be read sequentially, starting with the first field in the first record and proceeding from that point. Heres a brief example:
FileNum = FreeFile
Open BOOKLIST.DAT For Output As #FileNum
Write #FileNum, Henry James,The Aspern Papers,12
Write #FileNum, Dostoyevsky,Crime and Punishment,123
Close #FileNum
Then, in another part of the program:
Dim A As String, B As String, C As String, D As String
Dim x As Integer, y As Integer
FileNum = FreeFile
Open BOOKLIST.DAT For INPUT As #FileNum
Input #FileNum, A, B, x
Input #FileNum, C, D, y
Close #FileNum
After this code executes, A = Henry James, B = The Aspern Papers, x = 12, C = Dostoyevsky, D = Crime and Punishment, and y = 123.
The Input # statement reads data from the file on a field-by-field basis, assigning fields read from the file in the order in which variables appear in the Input # statements argument list. If you replaced the two Input # statements in the previous example with the single statement
Input #FileNum, A, B, x, C, D, y
or with the multiple statements
Input #FileNum, A, B
Input #FileNum, x, C
Input #FileNum, D, y
the results would be exactly the same. The way that Input # breaks file data into fields depends on the type of variable in the Input # statements argument list. If Input # is reading data into a string variable, the end of a field is marked by one of the following:
A double quotation mark if the field begins with a double quotation mark
A comma if the field does not begin with a double quotation mark
A carriage return-line feed (CR-LF)
If Input # is reading data into a numeric variable, the end of the field is marked by:
A comma
One or more spaces
CR-LF
When you are using a sequential file to store data, the program is responsible for keeping Write # and Input # statements synchronized in terms of the type, order, and number of fields in each record. In other words, data items must be read from the file in the same order they were written to it. If this synchronicity is disrupted, two kinds of problems can result:
The program might use Input # to read a field from a file into a variable of a different type (for example, reading string data into a numeric variable). No error happens when this occurs, but it will likely produce unexpected results. When a numeric field is read into a string variable, the variable is assigned the string representation of the number. When a string field is read into a numeric variable, the result depends on the string. If the string starts with a non-numeric character, the variable is assigned the value of zero. If the string starts with one or more numeric characters, the variable is assigned the numeric value of those characters.
If the number of fields is wrong, the program may lose track of its location within the file. Remember that the Input # statement counts fieldsnot recordswhen it reads data from the file. After one Input # statement executes, the next Input # statement starts with the next field in the file, which may not be the beginning of the next record.
One common use for sequential access files is to store an array of variable-length strings. This code fragment demonstrates how to write the array data to a disk file:
Dim notes(100) As String, count As Integer, FileNum As Integer
...
Code here puts data into the notes array.
...
FileNum = FreeFile
Open NOTES.TXT For OUTPUT As #FileNum
For count = 0 TO 100
WRITE #FileNum, notes(count)
Next count
Close #FileNum
The following code retrieves the data from the disk file and places it in a string array:
Open NOTES.TXT For INPUT As #FileNum
For count = 0 To 100
Input #FileNum, notes(count)
Next count
Close #FileNum
You can also use a sequential file to store arrays of numbers, but as youll see later in the chapter, another file access mode is actually better suited for this task.
Detecting The End Of The File
When your program is reading data from a sequential file, the EOF function enables you to detect when the program has reached the end of the file. The syntax is
EOF(filenum)
where filenum is the number associated with an open file. EOF returns True if the last record in the file has been read and False if it has not yet been read. Trying to read past the end of a sequential file will generate an error. The following code shows how to use EOF in a loop to read an entire sequential fileone line at a timeinto an array:
Dim info(1000) As String
Dim count As Integer
...
Open MYFILE.DAT For Input As #filenum
count = 0
While Not EOF(filenum)
Line Input #filenum, info(count)
count = count + 1
Wend
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:
sieci lab 13 03 08Centralne Laboratorium Kryminalistyczne Policji Wykaz zatwierdzonych specyfikacji 13 03 25Informatyka 13 03 2012Wykład 3 (13 03 2009) montażNOTATKI WYKLAD2 13 03 0913 03NOTATKI WYKLADI 13 03 09Kształcenie ruchowe – ćwiczenia nr 4 (13 03 12r )03 Rozdzial 10 13Plakat JELENIA GORA Przyjazdy wazny od 13 12 15 do 14 03 08WSM 03 13 plTI 02 03 13 T pl(1)Moc męki i śmierci Jezusa Nasz Dziennik, 2011 03 13TI 03 03 13 T B pl(1)więcej podobnych podstron