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. Balchen

What 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 File

In 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.

Open A Visual Basic command used to open files for editing
"C:\Windows\My File.txt" The path and name of the file you want to open
For Input Tells Visual Basic that you want to open the file so you can read from it
As #1 This is a little tricky. Each time you open a file, Visual Basic assigns a unique ID to that file. When you later perform operations on the file, you use that ID, not the file's name.

Instead of generating random IDs, Visual Basic lets us specify an ID that we want to use.

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:

For Input Read from the file
For Append Append (add) to the file
For Output Delete the file before writing to it

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 File

Now 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

   Open "C:\Windows\My File.txt" For Input As #1
      Line Input #1, Data

The underlined code is the new code. Let us examine it:

Line Input Tells Visual Basic to read one line of characters (ended by a carriage return and line feed)
#1, We want to read from the file which was assigned ID 1 when we opened it
Data The data Visual Basic reads should be put in the variable Data

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

   Open "C:\Windows\My File.txt" For Input As #1
      Line Input #1, Data
      Debug.Print Data
   Close #1

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:

File not found

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 File

Now 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
      Print #1, "This is a test"
   Close #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

   Open "C:\Windows\My File.txt" For Input As #1
      Line Input #1, DataDebug.Print "Line 1: " & Data
      Line Input #1, DataDebug.Print "Line 2: " & Data
      Line Input #1, DataDebug.Print "Line 3: " & Data
      Line Input #1, DataDebug.Print "Line 4: " & Data
   Close #1

You might have noticed that if you tried this on a file with less than 4 lines, you get an error:

Input past end of file

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 Section

Section II will cover the end-of-file issue, reading entire files using loops, and reading record-style files.