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
Defining Random File Record Structure
The structure of a random files records is defined in the same manner as the user-defined data type created with the Type...End Type statements covered in Chapter 4. The first step is to create a user-defined data type that has the structure you want to use for the random file. This method works well, because almost all programs that use random files require the same user-defined data type to manipulate the data in the program. Continuing our earlier example of a filing system for books, you could write:
TYPE Book_Data
Author As String * 20
Title As String * 25
Index As Integer
END TYPE
DIM Books(100) As Book_Data
Now you have a user-defined type that will manipulate the book data in the program, as well as define the record structure for the random access file. When opening a file for random access, one of the required parameters is the length of the files records (you saw this earlier in the chapter in the discussion of the Open statement). This task is easily accomplished using the Len function, which returns the length, in bytes, of a data type. The Open statement would read:
filenum = FreeFile
Open BOOKFILE.DAT FOR RANDOM AS #filenum Len = Len(Books(1))
Note that you passed the Len function one array element, not the entire array. This statement opens the random file with a record length equal to the size of the user-defined data structure.
Reading And Writing Random Files
To write data to a random file, use the Put statement. The syntax is
Put [#]filenum,[recnum],usertype
with the arguments as follows:
filenumThe number associated with the random file when it was opened.
recnumThe record number in the file where the data is to be placed. If recnum is omitted, the next record (the one following the position of the last Get or Put) is used. If no Get or Put has been executed since the file was opened, record 1 is used.
usertypeA structure of the user-defined type that contains the data to be written.
Continuing with the book-filing example, to write data to the first record in the file, you would write:
Books(1).Author = Henry James
Books(1).Title = The Aspern Papers
Books(1).Index = 12
Put #FileNum, 1, Books(1)
When first creating a random access file, you can start writing data at record 1, increasing the record number by 1 for each successive Put statement. You could also omit the recnum argument in the Put statement, and let Visual Basic automatically keep track of record numbers. If you are adding records to an existing file, however, you must control recnum to ensure that the data is placed where you want it. If you write new data to a record number that is already in use in the file, the new data will replace the old data. If you dont want to overwrite existing data, you must add new records at the end of the file. In other words, for a file that already has n records, start adding new records at record n + 1.
How do you determine the number of records in a file? You use the Lof function, which returns the file length, or size, in bytes. Its syntax is
Lof(filenum)
where filenum is the number associated with the open file whose length you want to know. What is the benefit of knowing the files total size? You already know that the Len function returns the size of each record in bytes. It follows that the total bytes in a file divided by the bytes per record will give us the number of records in the file.
Therefore, you would write:
nextrec = (Lof(1) \ Len(usertype)) + 1
Put #FileNum, nextrec, usertype
To obtain the number of the next record to be read or written, use the Seek function. Similarly, the Loc function returns the number of the last record read or written.
To read data from a random access file, use the Get statement. Data is read from a specified record in the file and placed in a user-defined structure of the type used to create the file. The syntax of the Get statement is
Get [#]filenum,[recnum],usertype
with the arguments:
filenumThe number associated with the random file when it was opened.
recnumThe number of the record to be read. If recnum is omitted, data is read from the next record (the one after the last Get or Put ). If no Get or Put has been executed since the file was opened, record 1 is read.
usertypeA variable of the user-defined type where the data is to be placed.
Again using the book-filing example, to read the first record of a random file, you would write:
GET #FileNum, 1, Books(1)
To read all records from a random file, use a loop that starts with the first record and reads sequential records until the end of the file is reached, placing them in the elements of an array of the appropriate user-defined type. To detect the end of file, use the EOF function, as shown earlier in this chapter for sequential files. You can also calculate the number of the last record in the file by dividing the file length by the record length, as shown previously.
Visual Basic maintains a file pointer for each open random access file. This pointer specifies the record number that will be read or written by the Get or Put statement if the recnum argument is omitted. When a random access file is first openedwhether its a new file or an existing filethe file pointer initially points at record 1. As you read and write random file data, the file pointer is maintained as follows:
A call to Get or Put without a recnum argument increments the file pointer by 1.
A call to Get or Put with a recnum argument sets the file pointer to (recnum + 1).
To determine the current file pointer position, you can use the Seek and Loc functions. Seek returns the position of the file pointer (the next record to be read or written). Loc returns the position of the last record that was read or written. The syntax is
Seek(filenum)
Loc(filenum)
where filenum is the number associated with an open random access file. The value returned by Loc is one less than the value returned by Seek. When a file has just been opened, Loc returns zero and Seek returns one.
Using Binary Files
A binary file stores data as an unformatted sequence of bytes. No record/field structure exists in the file (unless you impose one in code). Binary files provide a great deal of flexibility in storing data, but their unstructured nature forces the programmer to keep track of what is stored in the file. That necessitates keeping track of where you are in the file.
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 06 Ciecie i spawanie metaliwolyn 13 06 22 prodPrzekazywanie ciepła S Pietrowicz 13 06 2015 temrin IIcennik modemow i routera w ofercie Internetu CP ST 13 06 2011Afganistan 41 kandydatów w wyborach prezydenckich (13 06 2009)Rozporzadzenie MNiSW w sprawie kieruków studiów (13 06 2006)13 06 2012 interna pytania06 11 09 (13)06 1 13więcej podobnych podstron