Reading and Writing Files in Visual Basic

By Herbert J. Bernstein

© Copyright 1999 Herbert J. Bernstein

Computers store information in many ways. The registers within processors and random access memory (RAM) are the most immediately accessible storage. Any portion of that memory may be read or written on time scales commensurate the the rate of instruction execution. However, the information stored there may be lost when a computer is turned off. Disks and tapes are used for secondary storage. In general, it takes longer to access each portion of memory from a disk or tape than from RAM, but there is room for more information and the information can be preserved long term.

Modern computer operating system organize the information on secondary storage devices into "Files". If the file is organized so that it must be read or written in sequence, i.e. from beginning to end, the file is called a sequential file. If the file is organized so that we may go directly to any selected portion of the file, it is called a direct access file or a random access file. Depending on the particular operating system, subtle distinctions may be made among types of files. For example, some files may be organized to handle lines of text, while others may be organized to handle streams of binary data.

The statements to provide access to files in Visual Basic are of the form:


     Close #num
     Open filename$ For mode As #num
     Open filename$ For mode As #num Len = reclen
     Get #num, record_num, record_var
     Input #num, variable_list
     Line Input #num, line_string$
     Print #num, expression_list
     Put #num, record_num, record_var
     Write #num, expression_list

The Open and Close statements are used to start and end the association between a file on disk and a special file number identifier #num, which is used internally within the program. The valid modes are Output, Input, Append, Binary and Random. A file opened for Output is written from the beginning. A file opened for Input is read from the beginning. A file opened for Append is written from the end of the data previously written. A file opened for Binary may be written or read without any formatting. A file opened for Random permits the use of Put and Get for random access to records in arbitrary order. For both Binary and Random the Len field should be used to specify the length of each record in characters.

The modifier Lock may be added to a mode to provide an interlock on access by multiple processes. The mode Shared is used to allow shared access.

The function EOF with an argument equal to a file number (without the "#") returns true when a sequential file reachs end of file.

Let us look at a shell sort done to from one file to another.


Rem Sample of a File-based Shell Sort
Rem H. J. Bernstein, 17 November 1999

Rem Requires a Form with an output file name text box Text1
Rem                      an input file name text box Text2
Rem                      a scratch file text box Text3
Rem                      a sort button SortButton


Private Sub SortButton_Click()

  REM Set up error handling if file does not exist

  On Error GoTo ErrorHandler
 
  Dim FileName As String
  Dim MyString As String
  Dim String1 As String
  Dim String2 As String
  Dim RecNum As Integer
  Dim Range As Integer
  Dim Done As Integer
  Dim NameCount As Integer
  Dim Icount As Integer
  
  REM Open files

  FileName$ = Text1.Text
  Open (FileName$) For Input As #1
  FileName$ = Text2.Text
  Open (FileName$) For Output As #2
  FileName$ = Text3.Text
  RecNum = 0
  Open (FileName$) For Random As #3 Len = 255

  REM Copy each line of the input file to the temporary random file

  While Not EOF(1)
    Line Input #1, MyString
    RecNum = RecNum + 1
    Put #3, RecNum, MyString
  Wend

  REM Sort the Random file with a Shell sort
  
  NameCount = RecNum
  Range = Int(NameCount / 2)
  Do While (Range > 0)
    Do
    Done = 1
    For Icount = 1 + Range To NameCount Step Range
      Get #3, Icount - Range, String1
      Get #3, Icount, String2
      If (UCase(String1) > UCase(String2)) Then
        Put #3, Icount - Range, String2
        Put #3, Icount, String1
        Done = 0
      End If
    Next Icount
    Loop Until (Done = 1)
  Range = Int(Range / 2)
  Loop

  REM Copy the sorted file to the output file
  
  For Icount = 1 To NameCount
    Get #3, Icount, MyString
    Print #2, MyString
  Next Icount

  REM Close all files

  Close #1
  Close #2
  Close #3  
  Exit Sub
  
ErrorHandler:
  Select Case Err.Number
    Case 53
    MsgBox ("Unable to open " & FileName$)
  End Select
  Resume
End Sub

Private Sub Form_Load()
  Text1.Text = ""
  Text2.Text = ""
  Text3.Text = "C:\tmp\Sort.tmp"
End Sub