Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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

Bookmark It

Search this book:
 
Previous Table of Contents Next


In this example, we’ll just have one record, which we fill from the text boxes Text1 and Text2 when the user clicks a button:

Private Sub Command1_Click()
   WriteData(1).Name = Text1.Text
   WriteData(1).Number = Text2.Text
   TotalRecords = 1
...

Next, we create a file to store our record(s) in—note that we set the size of each record in the file with the Len keyword:

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))
...
FileError:
   MsgBox "File Error!"
End Sub

Finally, we use the Put statement to write the data to the file. We only have one record here, but if we had a number of records, we could loop like this:

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 loop_index = 1 To TotalRecords
       Put #1, , WriteData(loop_index)
   Next loop_index
   Close #1
   Exit Sub

FileError:
   MsgBox "File Error!"

End Sub

And that’s it—we’ve written our data file. To see how to read records back in, see “Reading From Random Access Files” later in this chapter.

Writing To A Binary File

You usually write records to binary files using the Put statement:

Put [#] filenumber, [recnumber], varname

Here, filenumber is the number of a file to write to, recnumber is the number of the record to write for random files and the byte at which to start writing for binary files, and varname is the name of the variable that holds the data to write to the file.

Here’s an example showing how to use Put to write a floating point number the user has entered in a text box, Text1, to a file—note that we open that file in Binary mode and don’t use a record number with Put here:

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

To see how to read the binary data back in, see “Reading from Binary Files” later in this chapter.

Reading From Sequential Files

To read from sequential file, you can use these standard statements (we’ll see how to use TextStream methods later in this chapter):

Input # number, expressionlist
Line Input # number, string
Input$ ( numberbytes, [#] number)

Here, number is a file number, expressionlist is a list of variables the data will be stored in, string is a string variable to store data in, and numberbytes is the number of bytes you want to read. Let’s see some examples.

The Input # Statement

You can use the Input # statement to read text and numbers from a sequential file. For example, if we write two integers the user has entered in Text1 and Text2 to a file, data.dat, this way using Write # when the user clicks Command1:

Private Sub Command1_Click()
   Open "c:\data.dat" For Output As #1
   Write #1, Val(Text1.Text), Val(Text2.Text)
   Close #1
End Sub

then we can read those integers back using Input # this way when the user clicks Command2:

Private Sub Command2_Click()
   Dim int1, int2 As Integer

   Open "c:\data.dat" For Input As #1
   Input #1, int1, int2
   Text3.Text = Str(int1)
   Text4.Text = Str(int2)
   Close #1

End Sub

The result appears in Figure 17.5. When the user enters two integers in the text boxes and clicks the Write Data button, we write them to disk. When the user clicks the Read data button, we read them back using Input # . In that way, we’re able to write and read a sequential file. The code for this example is located in the filedata folder on this book’s accompanying CD-ROM.


Figure 17.5  Using Write # and Input # to save and restore integers.

The Line Input Statement

Using the Line Input statement, you can read lines (text strings that end with a carriage return or carriage return/line feed pair) from a file. For example, say we had this set of lines, each separated by a carriage return/line feed pair in a file named file.txt:

Here is some
multi-line text
that we
will read in...

When the user clicks a button, we can read in the preceding text line by line with Line Input . First, we open the file:

Private Sub Command1_Click()

   On Error GoTo FileError
   Open "c:\file.txt" For Input As #1
...
FileError:
   MsgBox "File Error!"
End Sub

Now we need some way of looping over all the lines in the file—but how do we know when we’ve reached the end of the file? We use the Visual Basic EOF (End Of File) function, which returns True when we reach the end of the file:

Private Sub Command1_Click()

   On Error GoTo FileError
   Open "c:\file.txt" For Input As #1
   Do Until EOF(1)
...
   Loop

   Exit Sub

FileError:
   MsgBox "File Error!"
End Sub

Next we use Line Input to read lines of text from the file and append them to a multiline text box (that is, a text box with its MultiLine property set to True), Text1, along with a carriage return line feed pair this way:

Private Sub Command1_Click()
   Dim NewLine As String

   On Error GoTo FileError
   Open "c:\file.txt" For Input As #1
   Do Until EOF(1)
       Line Input #1, NewLine
       Text1.Text = Text1.Text + NewLine + vbCrLf
   Loop

   Exit Sub
FileError:
   MsgBox "File Error!"
End Sub

The result of this code appears in Figure 17.6. When the user clicks the command button, we read in the file.txt file line by line using Line Input and display it in the text box.


Figure 17.6  Reading text with Line Input.

The code for this is located in the fileread folder on this book’s accompanying CD-ROM.


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.