Classic VB has a built in set of keywords held over from the legacy BASIC era, used for dealing with IO. As you should be aware there are two directions of data flow, input and output. Let's look at both of these in application to text files.
The first thing we must do before working with a file is to open it. The Open statement takes the filename and the open mode, in which we specify how we're going to use the file. If we try and use it in a way we didn't specify, VB will slap us on the wrist.
To open a file you need a file handle which is a number uniquely identifying the opened file within the application workspace. To get the next available file handle we call the FreeFile() function.
VB Code:
Dim fileID As Long
fileID = FreeFile()
Let's create a file called "C:\example.txt". If this file already exists, it will be overwritten since we are going to specify that we want to write to it.
VB Code:
Open "C:\example.txt" For Output As #fileID
If you want, you can specify that you want to lock the file from being written to by other applications.
VB Code:
Open "C:\example.txt" For Output Lock Write As #fileID
Now the file is opened, we can perform operations on it. In Output mode we can do Print's and Write's. Write is used when treating a file like a mini database. It surrounds the text outputted with quote marks.
Let's print a simple line to the file.
VB Code:
Print #fileID, "I come from a land down under"
Simple
Once we're done with the file we must always close it.
This frees up the file handle and makes sure changes to the file are commited and it can be used by other applications.
Now let's look at reading from the file. There are two main ways we can do this, Input mode and Binary mode.
Input mode is best for reading one line at a time. To do this we use the Line Input statement, which reads the next line of the file into a buffer variable. The EOF() function returns True if we have hit the end of the file, so we loop while it is false to read all of the file.
VB Code:
Open "C:\example.txt" For Input Lock Write As #fileID
Dim line As String
Do While (Not EOF(fileID))
Line Input #fileID, line
MsgBox(line)
Loop
Close #fileID
Binary is good for reading the whole thing in one go, or chunks of the file when you know the start byte and byte length that you want to read. In Binary mode we can use the Input and Get statements. Both require a buffer variable, like Line Input. The Input statement sets the size of the buffer variable for you, whereas Get determines how much data to read by how long the buffer variable already is.
Here is an example of reading an entire file into a string. Here I am using the Get statement, which takes file handle, start byte (optional), and buffer variable. The amount of data read is determined by the length of the buffer. This method is very efficient.
VB Code:
Open "C:\example.txt" For Binary Access Read Lock Write As #fileID
Dim data As String
' Set the size of the data buffer to the size of the file
data = Space(LOF(fileID))
' Read the data
Get #fileID, , data
Close #fileID
Binary mode also allows you to write data. To do this we can use the Put statement. Put is simply the reverse of Get, it writes the data to the file at the specified starting position.
VB Code:
Open "C:\example.txt" For Binary Access Write Lock Read Write As #fileID
Dim data As String
data = "Test string"
' Read the data
Put #fileID, , data
Close #fileID
As ever if you have any further Q's please post in Classic VB. If you have any further explanations or examples then you can post in this thread.
Cheers
- P