Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 73,376 times

Contents

Related Categories

Files and Folders - Reading and writing to files

Reading and writing to files

Reading and writing files is one of the first things you will want to know how to do in Visual Basic, so here's how!

When you read and write files from Visual Basic, you always follow a number of steps:
1) Get a free file number using the FreeFile function. This is used to identify the file once you have opened the file.
2) Use the Open function to open the file for Input (read) or Output (write).
3) Perform an action on the file, either reading it or writing to it.
4) Close the file

To get a free file number, you use the FreeFile function:

' Get a free file number
nFileNum = FreeFile

Then, to open the file, we use the Open function. This takes the following syntax:

Open Filename For Method As FileNumber

Filename is the name of the file, Method is Input or Output (there are other methods, and these are discussed later), and FileNumber is a free file number. For example, the following code opens test.txt for Input:

' Get a free file number
nFileNum = FreeFile
' Open Test.txt for input. App.Path returns the path your app is saved in
Open App.Path & "\test.txt" For Input As nFileNum

If you aren't sure when to use Input and when to use Output, or, like me, can never remember which way round they go, take a look at the diagram below:

Next, we need to do something with it! To get some text from the file, we use the Input statement:

Input(Length, FileNumber)

So, if we wanted to load the first 100 characters from the file, we would use

Input(100, nFileNum)

where nFileNum is the file number we opened the file with.

You can then call the function again to read the next 100 characters and so on. If you want to move where you currently are in the file, use the Seek function:

Seek(FileNumber, Position)

For example, the following code loads the characters from 0 to 10, from 10 to 20, and then from 30 to 40:

sText = Input(10, nFileNum) 'first 10 chars
sText = sText & Input(10, nFileNum) 'next 10 chars
Seek nFileNum, 30 'go to char 30
sText = sText & Input(10, nFileNum) 'chars 30-40

If you want to get the whole file, you can use the LOF (Length of File) function. The following line returns the whole file:

Input(LOF(nFileNum), nFileNum)

If, on the other hand you want to write to the file, you use the Write function:

Write #nFileNum, Text

where, as usual nFileNum is the file number we opened the file with, and Text is the text we want to write to it. Please note that any text you write to the file will overwrite any existing text. If you don't want to do this, take a look at the Appending To Files section. Also note that each time you call the Write function, VB will automattically add a new line. So, if you write:

Write #nFileNum, "hello"
Write #nFileNum, "hello again"

you will get

hello
hello again

instead of

hellohello again

For those of you who are inquistive, and asking why we need the # before the file number, the answer is I don't know!

When you have finished with the file, you use the Close statement to close the file. VB does actually do this for you when your application closes, but it is a good habit to get into.

Close(nFileNum)

To put this all together, take a look at the following code. This reads the whole contents of Test.txt into TextBox1.

Dim nFileNum As Integer

' Get a free file number
nFileNum = FreeFile

' Open Test.txt for input. App.Path returns the path your app is saved in
Open App.Path & "\Test.txt" For Input As nFileNum

' Read the contents of the file into TextBox1
TextBox1.Text = Input(LOF(nFilenum), nFileNum)

' Close the file
Close nFileNum

and, to write the contents of TextBox1 back to the file, you use the following code:

Dim nFileNum As Integer

' Get a free file number
nFileNum = FreeFile

' Create Test.txt
Open App.Path & "\test.txt" For Output As nFileNum

' Write the contents of TextBox1 to Test.txt
Write #nFileNum, TextBox1.Text

' Close the file
Close nFileNum

Simple - Now, if you want to do some more advanced file reading/writing functions, go to the next page!

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