Using Files: Section II
In Section I, we discussed how to read one line a at time from a file, and also how to write to files. In this section, we will look at reading entire files and counting the number of lines in a file. You will also learn how to read record-style data.
Author: Jens G. Balchen

Before You Start

In order to read an entire file of unknown length, we use the Do...Loop structure. If you're not familiar with this, you should look it up in the Visual Basic help file.

You should also read Section I before you embark on this section.

Detecting The End of a File

In order to read an entire file, it's vital that we know when to stop reading. Since the number of lines is unknown, we must try to detect when we're going outside the bounds of the file.

Visual Basic provides us with the EOF function. It takes the unique ID of an open file (from here on, we will call this a file handle, like in the real world) as a parameter, and returns either True or False. We use it like this:

   EOF(file handle)

With this info, we could write a little program that reads all the lines in a file, on at a time, testing if we've reached the end-of-file for each line. It would look like this:

   Dim Data As String

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

... and so on. Try this on a 1 or 2 line file, and you will notice that it doesn't do any reading after the last line in the file.

Reading Entire Files

The previous approach works if you know the length of the file, but it's tiresome to write and very inefficient. Instead of doing this, we will use the Do...Loop structure.

   Dim Data As String

   Open "C:\Windows\My File.txt" For Input As #1
      Do While Not EOF(1)
         Line Input #1, DataDebug.Print "Line X: " & Data
      Loop
   Close #1

If you chose to read a large file, you will see the Debug Window being flooded with lines of text, all of them saying Line X:.

Let's do something else for a change. How about counting the lines in the file? If we want to do that, we need a new variable; the counter.

   Dim Counter As Long

In this code, we won't print the Data to the Debug Window, we'll only print the line count.

   Dim Data As String
   Dim Counter As Long

   Open "C:\Windows\My File.txt" For Input As #1
      Do While Not EOF(1)
         Line Input #1, DataCounter = Counter + 1
      Loop
   Close #1

   Debug.Print "Line count: " & Counter

The line Counter = Counter + 1 increases the line count with 1 each time we read a line. When the code is run, you will only see one line in the Debug Window; the line saying Line count: n.

Reading Record-Style Files

A record is a collection of separate data fields. In our case, a data field is one line in a file. How can we divide a file into records, and how do we read them?

The answer is simple. We use the Do...Loop structure from earlier, but instead of reading 1 line inside the loop, we read as many lines as the record contains fields. In our example, we have a record consisting of three fields: Firstname, Lastname, and Age.

We also use a counter to keep count of the records.

   Dim Firstname As String
   Dim Lastname As String
   Dim Age As String

   Dim Counter As Long

   Open "C:\Windows\My File.txt" For Input As #1
      Do While Not EOF(1)
         Counter = Counter + 1
         Debug.Print "Record#: " & Counter

         Line Input #1, FirstnameDebug.Print "Firstname: " & Firstname
         Line Input #1, LastnameDebug.Print "Lastname: " & Lastname
         Line Input #1, AgeDebug.Print "Age: " & Age
      Loop
   Close #1

To try this example, you need a file where the data is arranged in groups of three lines. If you wish, you can download one here (hold down Shift. If not, you will see it on screen). Remember to modify the filename to Records.txt instead of My File.txt -- you choose the folder.

Next Section

In Section III, we will look at other types of data, and how to read them from a file.