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
The Input$ Statement
The Input$ statement lets you read in a string of a specified length. It might seem odd to have to know the strings lengths before reading them in, but Input$ does have one very useful aspect: if you use it together with the LOF (Length Of File) function, you can read in a whole text file at once.
For example, heres how we read in the file from the previous example, file.txt, all at once, without having to work line by line:
Private Sub Command1_Click()
Dim NewLine As String
On Error GoTo FileError
Open "c:\file.txt" For Input As #1
Text1.Text = Input$(LOF(1), #1)
Exit Sub
FileError:
MsgBox "File Error!"
End Sub
This example produces the same result as the previous example that uses Line Input.
Reading From Random Access Files
The Testing Department is on the phone. Your new program, SuperDuperDataCrunch, is great for writing data to disk, but shouldnt you let the user read that data back in? Hmm, you think, good idea.
You use Get to read records from a random access file:
Get [#] filenumber, [recnumber], varname
Here, filenumber is the number of a file to read from, recnumber is the number of the record to read, and varname is the name of the variable that should receive the read-in data.
Lets see an example. Earlier in this chapter, we saw how to write records to a random access file. We set up a new type named Record in a module:
Type Record
Name As String * 50
Number As String * 50
End Type
Then we set up two formwide arrays of records, WriteData and ReadData, and an integer named TotalRecords to keep track of how many records are total (these variables are stored in the (General) section of the form):
Dim WriteData(1 To 50) As Record
Dim ReadData(1 To 50) As Record
Dim TotalRecords As Integer
When the user clicked a command button, we read the text from two text boxes, Text1 and Text2, placed that text in the first record of the WriteData array, and wrote that record out to a file named records.dat with the Put statement:
Private Sub Command1_Click()
WriteData(1).Name = Text1.Text
WriteData(1).Number = Text2.Text
TotalRecords = 1
On Error GoTo FileError
Open "c:\records.dat" For Random As #1 Len = Len(WriteData(1))
For intLoopIndex = 1 To TotalRecords
Put #1, , WriteData(intLoopIndex)
Next intLoopIndex
Close #1
Exit Sub
FileError:
MsgBox "File Error!"
End Sub
Now well see how to read that record back in. First, we open the file records.dat for random access, setting the record size to the length of each array element:
Private Sub Command2_Click()
Open "c:\records.dat" For Random As #1 Len = Len(ReadData(1))
...
Then we use Get to read in the records:
Private Sub Command2_Click()
Dim intLoopIndex As Integer
Open "c:\records.dat" For Random As #1 Len = Len(ReadData(1))
For intLoopIndex = 1 To LOF(1) / Len(ReadData(1))
Get #1, , ReadData(intLoopIndex)
Next intLoopIndex
Next, we loop over all the records in the file (although we use LOF(1) / Len(ReadData(1)) to determine the number of records in the file, we could also loop until the EOF function is True):
Private Sub Command2_Click()
Dim intLoopIndex As Integer
Open "c:\records.dat" For Random As #1 Len = Len(ReadData(1))
For intLoopIndex = 1 To LOF(1) / Len(ReadData(1))
...
Next intLoopIndex
...
Then we close the file and display the Name and Number fields of the first (and only) record in two new text boxes, Text3 and Text4:
Private Sub Command2_Click()
Dim intLoopIndex As Integer
Open "c:\records.dat" For Random As #1 Len = Len(ReadData(1))
For intLoopIndex = 1 To LOF(1) / Len(ReadData(1))
Get #1, , ReadData(intLoopIndex)
Next intLoopIndex
Close #1
Text3.Text = ReadData(1).Name
Text4.Text = ReadData(1).Number
Exit Sub
FileError:
MsgBox "File Error!"
End Sub
When you run this program, as shown in Figure 17.7, the user can enter data into the two text boxes at left, click the Write To File button to write the data to a record in a file, then click the Read From File button to read the data back in and display that text in the two text boxes at right.
Figure 17.7 Writing and reading records to and from a random access file.
You can see the result in Figure 17.7. Now were reading records from random access files in Visual Basic.
The code for this example is located in the filerecord folder on this books accompanying CD-ROM.
Reading From Binary Files
How do you read raw data from files that have been opened in Binary format with the Open statement? You usually use Get to read data from a binary file (although you can use Input # as wellsee the previous topic on reading from sequential files):
Get [#] filenumber, [recnumber], varname
Here, filenumber is the number of a file to read from, recnumber is the number of the record to read for random files and the byte at which to start reading for binary files, and varname is the name of the variable that will hold the read-in data.
Lets see an example. In this case, we first write some binary datasuch as a floating point numberto a file, and then well read it back in. Here, we let the user enter a Double value in a text box, which we read in when the user clicks a command button, Command1:
Private Sub Command1_Click()
Dim varOutput As Double
varOutput = Val(Text1.Text)
...
Then we write that number out to a binary file, binary.dat (making it a binary file by opening it in Binary mode):
Private Sub Command1_Click()
Dim varOutput As Double
varOutput = Val(Text1.Text)
On Error GoTo FileError
Open "c:\binary.dat" For Binary As #1
Put #1, , varOutput
Close #1
Exit Sub
FileError:
MsgBox "File Error!"
End Sub
Now its up to us to read that number back in as binary data when the user clicks a new button, Command2 . We start by opening the file again:
Private Sub Command2_Click()
On Error GoTo FileError
Open "c:\binary.dat" For Binary As #1
...
FileError:
MsgBox "File Error!"
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:
559 562562 pl wyklady z dnia lutego 11557 (2)557 559554 557554 557I CKN 562 97demo cgi 562README (562)562 567557,5,artykulwięcej podobnych podstron