Export (0) Print
Expand All

Reading from and Writing to a File Opened for Binary Access

Visual Studio .NET 2003

Binary access provides a number of advantages for certain tasks. For example, you can store information in a specific format, such as .zip, .mpeg or .xls. Binary access is similar to random access except that no assumptions are made about data type or record length. Because binary access does not require fixed-length fields, you can write variable-length fields to such a file. Binary access can thus keep file size smaller than random access because each field can occupy only the space it requires.

To open a file for binary access, specify OpenMode.Binary with the FileOpen statement. After it has been opened, you can use the same functions to write to the file, such as FilePut and FilePutObject, that you would with a file opened for sequential or random access. To read from the file, use FileGet and FileGetObject functions. For more information, see File Access Types and Functions.

Security Note   When reading from files, do not make decisions about the contents of a file based on the file name extension. For example, a file named Form1.vb may not be a Visual Basic .NET source file.

The following example uses the FilePut function to write a string to a file opened for binary access. It assumes that there is a file named test.txt in the current directory.

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
   System.EventArgs) Handles Button1.Click
      Dim MyString As String
      Dim ReadString As String
      Dim iFr As Short
      iFr = FreeFile()
      ReadString = CurDir() & "\test.txt"
      FileOpen(iFr, 1, OpenMode.Binary)
      MyString = "new information"
      FilePut(MyString)
      FileClose(iFr)
   End Sub

The following example uses the FileGet function to display the first 15 characters contained in test.txt. Again, this example presupposes that test.txt exists.

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
   System.EventArgs) Handles Button1.Click
        Dim MyString As String
        Dim ReadString As String
        Dim iFr As Short
        iFr = FreeFile()
        ReadString = CurDir() & "\test.txt"
        FileOpen(iFr, ReadString, OpenMode.Binary)
        MyString = New String(" "c, 15)
        FileGet(iFr, MyString)
        FileClose(iFr)
        MsgBox(MyString)
   End Sub

You may wish to use information gathered from a file in your code. For example, the following code sample writes the contents of a textfile, test.txt, to a listbox, myListBox. It assumes that the file, test.txt, exists and contains several items separated by carriage returns.

   Dim myListbox As ListBox
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
   System.EventArgs) Handles Button1.Click
      Dim ReadString As String
      Dim iFr As Short
      Dim sTmp As String
      ReadString = CurDir() & "\test.txt"
      iFr = FreeFile()
      FileOpen(iFr, ReadString, OpenMode.Binary)
      sTmp = Space(FileLen(ReadString))
      FileGet(iFr, sTmp)
      FileClose(iFr)

      Dim strRecords() As String : strRecords = Split(sTmp, vbCrLf)
      'Clear the listbox
      MyListbox.Items.Clear()
      Dim lngRecord As Long
      For lngRecord = LBound(strRecords) To UBound(strRecords)
         myListbox.Items.Add(strRecords(lngRecord))
      Next lngRecord 
  End Sub

See Also

Opening and Closing Files for Binary Access | Binary File Access | Random File Access | Sequential File Access | File Access with Visual Basic Run-Time Functions | FilePut Function | FilePutObject Function | FileGet Function | FileGetObject Function | File Access Types and Functions

Show:
© 2014 Microsoft