Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 73,274 times

Contents

Related Categories

Files and Folders - Binary file access

Binary file access

Binary access is the most basic way to read a file. It is called binary access because you read the file byte by byte (letter by letter, character by character).

To open a file using Binary Access you simply say For Binary, instead of For Input or Output etc. So,

Open App.Path & "c:\test.txt" For Binary As nFileNum

would open test.txt for binary access. To read characters from a file using binary access, you use the Get statement, which uses the following syntax:

Get FileNumber, ByteNumber, DestinationString

So, you pass the file number that you opened the file with, the byte to start at, and the string to put the result into. You will probably be wondering now how you specify how long a string to get. For binary access, you simply fill DestinationString with x number of spaces, where x is the number of characters you want to retrieve. For example,

sMyString = Space(7)

Get #nFileNum, 12, sMyString

would get 7 characters from the file (because sMyString is filled with 7 spaces), starting at byte 12 (the second parameter of Get). To write to a file using binary access, you use the Get statement:

Get FileNumber, ByteNumber, DestinationString

The following code opens a file for binary access and fills TextBox1 with bytes 12-18 of the file.

Dim nFileNum As Integer, sMyString As String
' Get a free file number
nFileNum = FreeFile

' Open the file test.txt for binary access
Open "test.txt" For Binary As nFileNum

sMyString = Space(7)

' Read 7 characters from the file, starting at byte 12
Get #nFileNum, 12, sMyString

' Close the file
Close nFileNum

' Fill TextBox1 with the string
TextBox1.Text = sMyString

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • help

    Posted by tieungao35 on 09 Nov 2005

    Hi all,
    I am a new beginner of Visual basic 6. Even though i read James's article, i don't know how to create a project and add his code as a new module.
    Could you please show me how to create 1...

  • INI files

    Posted by TomDooley on 26 Sep 2005

    Hello,

    the article of James is not state of the art.
    To import win32 functions is not the way how you should work with the .NET framework. With a little bit pation you can write a own managed code...

  • Copying folders including subdirectories

    Posted by kingman29 on 07 Jul 2005

    I am using deppfreeze in my internet cafe.This program protects hard drive from every changes and does not allow save games.When computer restarted all saves dissapear from hard drive.I know the game...

  • ei.

    Posted by po_tph on 20 Mar 2005

    do you a source code of hing folders???

  • hide folder source code???

    Posted by po_tph on 20 Mar 2005

    can anybody plz help me i want to have a sourcec code of hidng folders in windows directory.thanks