Using Files: Section I
Files are the second-most important thing a programmer interacts with (second only to the user). You can store vital information, databases, setup information, and countless other things in files. This introduction will cover basic file IO in Visual Basic. You will learn how to read and write ordinary strings to files, and how to manipulate information stored in files.
Author: Jens G. BalchenWhat Is a File?To a Visual Basic programmer, a file is a large block of bytes you can read from or write to. It is identified through its name, which is made from a set of folders and a file name like in Windows and DOS.There's nothing magical about a file. It is simply a container for information -- a way to separate one block of bytes from another. How To Edit a FileIn order to edit (read from or write to) a file, you will need to open it. That way, you tell Visual Basic, and ultimately Windows, that you want to edit the file. To do this, you use the Open command, like this:Open "C:\Windows\My File.txt" For Input As #1 Let us take a look at that code line.
But what if we wanted to write to the file instead of reading from it? The For <...> parameter takes several arguments, and the three we will be using here are:
As you probably noticed, Append and Output are similar, with the exception that Output deletes the contents of the file before adding to it. Reading Data From a FileNow that the file in the previous code sample was opened, we want to read something from it. To do that, we need a variable to store the data and a command to read from the file.
Dim Data As String
The underlined code is the new code. Let us examine it:
OK, so now the first line in the file has been read and placed in the variable Data. If you have tried this example on a file that really exists, you can step through the code using F8 (but stop after the Line Input), switch to the Immediate Window (Ctrl-G) and type Debug.Print Data. The first line of the file will appear in the window. When we're done reading, we should close the file, telling Visual Basic that we have no intention of editing it further. The command to do that is Close. It takes the unique ID as a parameter, and works like this: Close #1 The entire code would look like this:
Dim Data As String
The first line of the file will appear in the Debug window (Ctrl-G). If you haven't changed the filename, you will probably see this error message on your screen now:
That simply means that Visual Basic tried to open the file, but it wasn't there. If you don't know of any files in your Windows directory, try using "C:\autoexec.bat" instead. Writing Data to a FileNow that you know how to read the first line from a file, you might also want to know how to add a line to a file. The procedure is simple, but we need one new command to do the writing. It is called Print.Print takes two parameters; the unique ID of the open file and the data to write. We use it like this:
Print #1, Data In this case, Data can be a variable or it can be a text in quotes. If we wanted to write a new line to the end of a file, we would use the For Append statement with the Open command. So, here goes:
Open "C:\Windows\My File.txt" For Append As #1 If the file didn't exist earlier, it does now. What About Several Lines?If you want to read lines 1, 2, 3 and 4 from the same file, all you have to do is put 4 Line Input commands in a row. You can use 4 different variables to store the lines, or you can use the same variable each time. Keep in mind that reading data into a variable will erase the previous contents of the variable.Here's how we read 4 lines from a file:
Dim Data As String
You might have noticed that if you tried this on a file with less than 4 lines, you get an error:
This means that Visual Basic didn't find enough lines to read, hence tried to read past the end of the file. You will learn to deal with this in later sections. Next SectionSection II will cover the end-of-file issue, reading entire files using loops, and reading record-style files. |
||||||||||||||||||||||
Editor: Jens G. Balchen Last update: 2024-12-21 Copyright 1995-2024 VBI |