Functions of date and time
1.20. Work with text files
1.20. Work with text files
A file is an area on a hard disk, compact disk, USB flash drive (UFD) or any other medium that:
contains single-type information;
has a name.
When working with files, we will use operations for reading information from a file and writing information into a file.
Several types of files exist; we will consider text files. The contents of such file are lines of characters with a combination of characters “return” and “new line” at the end of each line.
For viewing text files, we will use the Notepad editor. At its window, we will not see characters “return” and “new line”.
To start working with a text file, operator Open is used for opening this file.
This operator has the following syntax:
Open name For purpose As number
In this construct, name is the file full name, i.e., the file name together with its path (in the file system of Windows) and extension, number is the file number, purpose is keyword Input, Output or Append (from “appending”).
The last three keywords have the following sense:
Input means that the file must be opened for reading information from this file;
Output — the file must be opened for writing information into it;
Append — the file must be opened for adding information into it.
As the file number, we recommend to use the number variable of the Integer data type whose value is the result of executing the following assign-ment operator:
number = FreeFile
where FreeFile is the built-in function that returns (into the program) the free file number.
The last assignment operator must be placed above the Open operator, more precisely, it must be executed before the Open operator.
After finishing the work with the file, it must be closed by the Close opera-tor as follows:
Close number
For addition of new lines into the file, operator Print is used, which has the following syntax:
Print #number, line1
where line1 is a string (the digit may be different).
Let the file with specified number be opened by means of the Output keyword. When performing the Print operator, string line1 (with a combina-tion of characters “return” and “new line” at the end, not in inverted commas) is written into the file beginning. When repeated performing the Print operator, string line2 is added into the file, and so on.
Let the file with specified number be opened by means of the Append keyword. When performing the Print operator, line1 is added into the file.
Below, we will consider two ways of extracting information from the file with specified number, which is opened by means of the Input keyword.
1. Extracting information by using operator LineInput with the follow-ing syntax:
Line Input #number, variable
This operator reads the next line from the file, at that, this line (without “return”
and “new line” at the end) is assigned to variable of the String data type.
2. Extracting information by means of built-in function Input(quantity, number)
This function returns (into the program) the string, which contains the subse-quent characters from the file. The quantity of these characters is specified by quantity.
In programs Creation and Addition, given below, we will use the re-viewed operators and built-in functions intended for work with text files. Using these programs as examples, we will also consider other useful operators and functions.
1.20. Work with text files
'Determining quantity of characters in file a.txt:
n1 = LOF(FNum1)
Close FNum1 'closing file a.txt MsgBox "In file a.txt" & Str(n1) & " characters"
'Copying information from file a.txt to file b.txt:
FNum1 = FreeFile
Close FNum1 'closing file a.txt 'Adding new string into file b.txt:
strA = "New string is added."
Print #FNum2, strA
'Determining quantity of characters in file b.txt:
n2 = LOF(FNum2)
In operators 1, 2 and 3, usr is the computer user name. Before the program execution, the reader should type his concrete user name instead of usr in these operators.
During the program execution, operator MkDir("c:\Users\usr\texts")
creates folder texts inside folder usr; MkDir is the abbreviation of “make directory”.
The built-in LOF function returns into the program the quantity of characters in the file, at that, characters “return” and “new line”, which are at the end of each line, are taken into consideration. The argument of this function is the file number; LOF is the abbreviation of “length of file”.
The argument of the built-in EOF function is the file number too. This func-tion, figuring in the condition of the DoUntil…Loop cycle termination, returns (into the program) True at achievement of the file end. The function name is the abbreviation of “end of file”.
The MsgBox procedure is intended for depicting the string, which is its
1.20. Work with text files
Fig. 1.19
Fig. 1.20 File a.txt:
Text file is created,
it contains several strings.
File b.txt:
Text file is created,
it contains several strings.
New string is added.
We advise the reader to look through the contents of files a.txt and b.txt by using the Notepad editor to be convinced of the correctness of the program work.
Before restarting the Creation program, the MkDir operator of creating the texts folder must be omitted, for example, by means of an apostrophe.
The liquidation of files a.txt and b.txt and folder texts may be required at the end of the program execution. For this purpose, the following operators are used:
Kill(FName1) Kill(FName2)
RmDir("c:\Users\usr\texts")
where RmDir is the abbreviation of “remove directory”. We must insert these operators above operator EndSub.
At the end of this section, we will consider how to tune Windows Explorer for displaying not only the file names, but also the extension of these names, for example, extension .txt for the above text files.
The following second program adds a line into file b.txt and runs the Notepad editor.
Listing 1.6 Sub Addition()
Dim FNum As Integer, n As Long
Dim RetVal As Integer 'for function Shell FNum = FreeFile
'Opening file b.txt and adding new string:
Open "c:\Users\usr\texts\b.txt" _ For Append As FNum
Print #FNum, "Second new string is added."
'Determining quantity of characters in file b.txt:
n = LOF(FNum)
Close FNum 'closing file b.txt MsgBox "In file b.txt" & Str(n) & " characters"
'Starting editor Notepad:
0: RetVal = Shell("c:\Windows\notepad.exe",1) End Sub
The Shell function (operator 0) is intended for running any executable file whose name has extension .exe. The first argument of the Shell function is the full name of the executable file (here, the full name must be without spaces). The second argument, which may be omitted, defines the style of the window being a result of calling the Shell function.
To see the full information on the Shell function, we must press the F1 key when the blinking cursor is located on Shell in the code window.
1.20. Work with text files
We will run the Addition program after the execution of the Creation program (without the operators liquidating the files and folder).
The window, shown in Fig. 1.21, appears during the Addition program execution. After clicking on OK in this window, operator 0 is executed, which starts the Notepad editor (Fig. 1.22), and then operator EndSub is executed, i.e., the Addition program is terminated.
Fig. 1.21
Fig. 1.22. The Notepad window
After terminating the Addition program, the Notepad window remains open. By means of this window, we can view the contents of a text file (in par-ticular, b.txt) and, if necessary, create a new file or edit an existing file.
After the Addition program execution, file b.txt has the following con-tents:
Text file is created,
it contains several strings.
New string is added.
Second new string is added.
If a file is opened for adding information (by using keyword Append), this file may not exist. In this case, the file is created. If a file is opened for reading information (by using keyword Input), this file, naturally, should exist.
To see file names with extension in Windows Explorer, we must fulfill the following:
1) in Windows Explorer, open the folder, which contains files of interest;
2) on the menu bar of Windows Explorer, fulfill Organize > Folder and search options;
3) in open window Folder Options, activate tab View;
4) in list Advanced settings, turn off option Hide extensions for known file types;
5) successively click on buttons Apply and OK.
To expand the chosen operation mode of Windows Explorer to all folders, we must click on button Apply to Folders before clicking on the Apply button. In open window Folder Views, we must click on the Yes button.
We advise the reader to write a program for creating a text file including 1
n lines of the following form:
i
i f
x )
f( ,
where x , i f are the values of argument and function, i 0 i n; a x0 < x < 1 x < ... < 2 xn 2 < xn 1 < xn b. Function f(x) and corresponding values of a and b from Appendix 4 must be used.
The constructs of Visual Basic considered above can be used for solving sufficiently complicated tasks, two of which will be formulated in the next section.
1.21. Matrix terminology. Formulation of demonstration tasks