6. Text files
A text file is a file containing lines of text. Let’s examine the creation and use of such files through examples.
6.1. Creation and use
Program |
![]() |
Results |
|
Comments
- Line 7 creates a file object of type "Scripting.FileSystemObject" using the CreateObject("Scripting.FileSystemObject") function. Such an object allows access to any file on the system, not just text files.
- Line 9 creates a "TextStream" object. The creation of this object is associated with the creation of the testfile.txt file. This file is not designated by an absolute path such as c:\dir1\dir2\....\testfile.txt but by a relative path testfile.txt. It will then be created in the directory from which the command to execute the file is launched.
- The Windows file system is not aware of concepts such as text files or non-text files. It only recognizes files. It is therefore up to the program that uses this file to determine whether to treat it as a text file or not.
- Line 9 creates an object using the SET command for assignment. Creating a text file object involves creating two objects:
- the creation of a Scripting.FileSystemObject (line 7)
- followed by the creation of a "TextStream" object (text file) using the OpenTextFile method of the Scripting.FileSystemObject, which accepts several parameters:
- the name of the file to be handled (required)
- the file access mode. This is an integer with three possible values:
- 1: read-only mode
- 2: write mode. If the file does not already exist and if the third parameter is present and has the value true, it is created; otherwise, it is not. If it already exists, it is overwritten.
- 8: append mode, i.e., writing to the end of the file. If the file does not already exist and the third parameter is present and set to true, it is created; otherwise, it is not.
- Line 11 writes a line of text using the WriteLine method of the created TextStream object.
- Line 13 "closes" the file. You can no longer write to or read from it.
- Line 16 creates a new "TextStream" object to use the same file as before, but this time in "append" mode. The lines that will be written will be appended to the existing lines.
- Line 18 writes two new lines, noting that the vbCRLF constant is the line-end marker for text files.
- Line 20 closes the file again
- Line 23 reopens it in "read" mode: we will read the file's contents.
- Line 27 reads a line of text using the ReadLine method of the TextStream object. When the file is first "opened," the cursor is positioned on the first line of text. Once this line has been read by the ReadLine method, the cursor moves to the second line. Thus, the ReadLine method not only reads the current line but also automatically "moves" to the next line.
- To read all lines of text, the ReadLine method must be applied repeatedly in a loop. This loop (line 26) ends when the AtEndOfStream attribute of the TextStream object is set to true. This means there are no more lines to read in the file.
6.2. Error Cases
There are two common error cases:
- opening a file for reading that does not exist
- Opening a file for writing or appending to a file that does not exist, with the third parameter set to false in the call to the OpenTextFile method.
The following program shows how to detect these errors:
6.3. The Tax Calculation application with a text file
We return to the tax calculation application, assuming that the data needed to calculate the tax is in a text file named data.txt:
12620 13190 15640 24740 31810 39970 48360 55790 92970 127860 151250 172040 195000 0
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65
0 631 1290.5 2072.5 3309.5 4900 6898.5 9316.5 12106 16754.5 23147.5 30710 39312 49062
The three lines contain, respectively, the data from the application’s limit, coeffR, and coeffN arrays. Thanks to the modular design of our application, changes are made primarily in the getData procedure responsible for constructing the three arrays. The new program is as follows:
Program |
|
Comments:
- In the text file data.txt, values may be separated by one or more spaces, making it impossible to use the split function to retrieve the values from the line. A regular expression had to be used
- The getData function returns, in addition to the three limit arrays, coeffR and coeffN, a result indicating whether an error occurred or not. This result is a Variant array of two elements. The first element is an error code (0 if no error), the second is the error message if an error occurred.
- The getData function does not validate the values found in the data.txt file. In a real-world scenario, it should do so.
