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!
Comments