** Please see next post for an update of code.
**
Open Statement
File IO in visual basic is initiated by the Open statement.
Syntax for the Open statement is as follows ;
Open pathname For mode [Access access] [lock] As [#]filenumber [Len=reclength]
It looks quite complicated, but in fact it isnt.
If we want to read data from a file, we would open it as follows ;
VB Code:
Open "c:\somefile.vbw" For Input As #1
If on the other hand we wanted to write to the file then ;
VB Code:
Open "c:\somefile.vbw" For Output As #1
There are 3 other ways of opening files.
VB Code:
Open "c:\somefile.vbw" For Append As #1
Open "c:\somefile.vbw" For Binary As #1
Open "c:\somefile.vbw" For Random As #1
If you do not specify how the file is to be opened,
Visual Basic will assume Random mode.
Close Statement
When we've finished working with our file we should close it.
Reason being that we might forget to later in our code,
or if we try to open another file with the same file number,
the we'll get a runtime error.
So we close a file as follows :
It doesnt matter what File IO mode we were in, Close #filenumber
applies for each mode.
Here are some examples :
VB Code:
Open "c:\somedir\myFile" For Output As #1
Close #1
Open "\\someserver\somefolder\somefile" For Random As #1
Close #1
It is normally good practice, that when you put an Open statement
into your code, you also at the same time put a Close statement
below it. You then fill in the code in the middle with the actual
file IO work that you want done.
The IO Work Itself
By now we should know how to both Open and Close any given file.
But we don't yet know how to actually do anything with the file.
So how do we read data from the file ?
There are a number of ways. Starting off with the simplest would
be using the Line Input statement.
VB Code:
Dim strBuff As String
Open "c:\autoexec.bat" For Input As #1
Line Input #1, strBuff
'strBuff now contains the first line of autoexec.bat
Close #1
Subsequent calls of the Line Input statement will
cause subsequent lines of the opened file to be read in.
So its obvious then that if we do many subsequent calls of this statement
we could read in the entire file ;
VB Code:
Dim strBuff As String
Open "c:\autoexec.bat" For Input As #1
Do
Line Input #1, strBuff
Debug.Print strBuff
Loop
Close #1
The above code will read in the entire file, but there is no condition
in that Do Loop so the code will keep trying
to read in lines from the file. For those of you interested, Visual Basic
will throw runtime error 62 : Input past end of file.
So how do we keep reading lines in from the file, while we're not at
the End of the file ? Or to put it another way, how do we keep reading
lines in from the file until we reach the end of the file ?
Well there is a function EOF() which will return a
boolean value {True,False} indicating whether or not we've reached the
end of the file. Here's an example of how to use it :
VB Code:
Dim strBuff As String
Open "c:\autoexec.bat" For Input As #1
Do Until Eof(1)
Line Input #1, strBuff
Debug.Print strBuff
Loop
Close #1
So the above code will keep reading in data until the end of the file.
Each line of the file is read into strBuff though.
But what if we wanted to store the information in an array so that we
can access the data later ? Well for this the obvious approach would
be to use a Dynamic Array ;
VB Code:
Dim strArray() As String
ReDim strArray(0)
Open "c:\autoexec.bat" For Input As #1
Do Until EOF(1)
Line Input #1, strArray(UBound(strArray))
ReDim Preserve strArray(UBound(strArray) + 1)
Loop
Close #1
So the entire file is stored, line by line, in the strArray array.
But there's an even faster way of doing this.
There is another File IO function called Input().
This will read in a specific number of bytes of a file into a String.
So we can read the entire file into a String buffer using this function.
All we need is a way of finding out the size of the file.
This is where the LOF() functions comes in.
LOF() will return the size of the file whose
file number is passed to the function.
So here are examples of how the functions work :
VB Code:
Open "c:\autoexec.bat" For Input As #1
MsgBox "Length of file : " & LOF(1)
MsgBox "First 20 bytes of file #1 : " & Input(20, 1)
Close #1
So then we could combine the two functions to read the entire file into
a buffer all at once :
VB Code:
Dim strBuff As String
Open "c:\autoexec.bat" For Input As #1
strBuff = Input(LOF(1), 1)
MsgBox strBuff
Close #1
So we now have the entire file in a String buffer.
And what if we wanted to store the String buffer as an array ?
Well imagine as file as being one really long line of text.
Instead of having different lines we have delimeters of vbCrLf characters.
So strBuff essentially contains one long String of text delimeted with
these special vbCrLf characters.
There's a very handy function in Visual Basic 6 that will take a String of
text, and given specific delimeters, will return an array.
The function is the Split() function.
Here's a few examples :
VB Code:
Dim strBuff() As String
Dim myString As String
myString = "a,b,c,d"
strBuff = Split(myString, ",")
MsgBox "Array element 0 : " & strBuff(0)
myString = "VB Forums|[url]http://www.vbforums.com[/url]|Blah!"
strBuff = Split(myString, "|")
MsgBox "Array Element 2 : " & strBuff(2)
myString = "a b c"
MsgBox "Array Element 1 : " & Split(myString, " ")(1)
So how do we do it with File IO ?
VB Code:
Dim strArray() As String
Open "c:\autoexec.bat" For Input As #1
strArray = Split(Input(LOF(1), 1), vbCrLf)
Close #1
So the above 3 lines of code will take the entire contents of a file,
read it in line by line, and store the data in an array.
This, as far as I know, is the best and fastest way of reading the contents
of a file into an array.