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. BalchenBefore You StartIn 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 FileIn 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
... 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 FilesThe 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
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 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 FilesA 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 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 SectionIn Section III, we will look at other types of data, and how to read them from a file. |
||
Editor: Jens G. Balchen Last update: 2024-11-08 Copyright 1995-2024 VBI |