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


The LOF Function

The LOF function returns the length of a file (in bytes) opened with the Open statement. You pass the LOF function an open file number. Here’s an example in which we report the length of a file we’ve just written, using the LOF function:

Private Sub Command1_Click()
   On Error GoTo FileError
   Open "c:\file.txt" For Output As #1
   Print #1, Text1.Text
   MsgBox "The file is" & Str(LOF(1)) & " bytes long."
   Close #1
   Exit Sub

FileError:
   MsgBox "File Error!"
End Sub

Opening A File

How do you open a file in Visual Basic? You use the Open statement. Here’s how the Open statement works:

Open pathname For mode [Access  access] [ lock] As [#] filenumber [Len= reclength]

Here are what the various arguments mean:

  pathname—A file name (may include directory or folder, and drive).
  mode—A keyword specifying the file mode: Append, Binary, Input, Output, or Random (if unspecified, the file is opened for Random access).
  access—A keyword specifying the operations permitted on the open file: Read, Write, or Read Write.
  lock—A keyword specifying the operations restricted on the open file by other processes: Shared, Lock Read, Lock Write, and Lock Read Write.
  filenumber—A valid file number in the range 1 to 511, inclusive. Use the FreeFile function to obtain the next available file number.
  reclength—Number less than or equal to 32,767 (bytes). For files opened for random access, this value is the record length. For sequential files, this value is the number of characters buffered.

If the file is already opened by another process and the specified type of access is not allowed, the Open operation fails and an error occurs. Also note that the Len clause is ignored if mode is Binary . If the file specified by pathname doesn’t exist, it is created when a file is opened for Append, Binary, Output, or Random modes. If you open an existing file for Output, it is overwritten; if you open it for Append, new data is added to the end of the file. After you’ve created the file, you refer to it using the file number.

For example, here we open a file named file.txt and write the contents of a text box, Text1, to that file:

Private Sub Command1_Click()
   On Error GoTo FileError
   Open "c:\file.txt" For Output As #1
   Print #1, Text1.Text
   Close #1
   Exit Sub

FileError:
   MsgBox "File Error!"
End Sub

Writing To A Sequential File

Sequential files are often text strings in Visual Basic, but they can also be combinations of text and numbers. You usually use these standard statements to write to sequential files in Visual Basic (we’ll also see how to use the TextStream methods later in this chapter):

Print # number, expressionlist
Write # number, expressionlist

Here, number is an open file number and expressionlist is a list of variables to write, separated by commas. Let’s take a look at some examples.

The Print # Statement

If you want to store your data in text format, use Print # . As an example, we’ll store the text in a text box to a file named file.txt using Print # . We start by checking for errors:

Private Sub Command1_Click()
   On Error GoTo FileError
...
FileError:
   MsgBox "File Error!"
End Sub

Then we open a file for output:

Private Sub Command1_Click()
   On Error GoTo FileError
   Open "c:\file.txt" For Output As #1
...
FileError:
   MsgBox "File Error!"
End Sub

Then we print the text in a text box, Text1, to the file:

Private Sub Command1_Click()
   On Error GoTo FileError
   Open "c:\file.txt" For Output As #1
   Print #1, Text1.Text
...
FileError:
   MsgBox "File Error!"
End Sub

Finally we close the file:

Private Sub Command1_Click()
   On Error GoTo FileError
   Open "c:\file.txt" For Output As #1
   Print #1, Text1.Text
   Close #1
   Exit Sub

FileError:
   MsgBox "File Error!"
End Sub

And that’s it—now the user can write the contents of a text box out to disk. The code for this is located in the filewrite folder on this book’s accompanying CD-ROM.

The Write # Statement

You can also use the Write # statement to write text and other types of data to a file. You use this statement with a file number and a comma-delimited list of the variables you want to write to that file. For example, here we open a file, data.dat, and write two numbers that the user has entered in the text boxes Text1 and Text2 to that file:

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

To see how to read those values back in, take a look at “Reading From Sequential Files” coming up in this chapter.

Writing To A Random Access File

You usually write records to random access 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 (you set the record size when you open the file), and varname is the name of the variable that holds the data to write to the file.

To work with records in a random access file, you define a record type first. For example, here we define a new type named Record in a module (you can only define types in modules; to add a new module to a program, use the Project menu’s Add Module item):

Type Record
    Name As String * 50
    Number As String * 50
End Type

Note that we use fixed-length strings here to make all our records the same size.

Now in a program, we can set up an array of such records in the (General) part of a form, as well as an integer to keep track of the total number of records:

Dim WriteData(1 To 50) As Record
Dim TotalRecords As Integer


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.