File processing
Solutions of tasks connected with processing of binary files with
numeric data and also
string files and text files are described on this page.
Binary file with numeric data: File48
This section contains description of solving the following task:
File48°. Three files of integers called SA, SB,
SC and a string SD are given. All given files are
of the same size. Create a new file called SD; this file must contain triples of components of the given files as follows:
A1, B1, C1,
A2, B2, C2, ... .
Creating a template and acquaintance with the task
To create a template of the required task one should use PT4Load tool:
Option Explicit
Public Sub Solve()
Task "File48"
End Sub
When the program is launched
you will see the Programming Taskbook window:
The first line of the input data panel contains four file names:
SA, SB, and SC
for input files, SD for an output file.
The next three lines of the input data panel displays
components of the input files.
File components are light-cyan colored to distinguish them from standard input-output data and comments.
With mouse or keyboard you can scroll the file components .
All input files are created with new name and data for each test running of the program.
When the program finishes all files are removed automatically from the working directory.
The running of our program is considered as acquaintance running because
the program does not perform input-output operations.
In the panel of results the "Example of right solution" tab is active;
this tab displays components that should be saved in the output file.
Initial data input
Before solving the task it is necessary to input file names
and open files by means of the Open statement. We shall use array named f to store file numbers:
Public Sub Solve()
Task "File48"
Dim s As String, f(1 To 4) As Integer, _
a As Integer, i As Integer
For i = 1 To 3
GetS s
f(i) = FreeFile
Open s For Random As f(i) Len = Len(a)
Next
Close
End Sub
Note that the Close statement provides closing all open files in the program.
Because we input only three file names, the next program running will lead to the error message
"Some required data are not input.".
Creating an empty output file
To create an output file we should use the same Open statement that was used to open existing files.
Thus, to correct the last error it is enough to replace 3 by 4
in the For statement:
Public Sub Solve()
Task "File48"
Dim s As String, f(1 To 4) As Integer, _
a As Integer, i As Integer
For i = 1 To 4
GetS s
f(i) = FreeFile
Open s For Random As f(i) Len = Len(a)
Next
Rem
Close
End Sub
The Rem comment marks the program position, where we can execute input-output file operations:
files are already opened by the Open statement and are not closed by
the Close statement.
Running of this program provides creation of an output file. But this file will be empty,
that is, will contain no elements. Therefore we shall see the error message
"Wrong solution", and the output data panel will contain the following text:
EOF:
This text (EOFEnd Of File) means that the output file exists but contains no elements.
Using of wrong type for file components
We did not use file input-output operations so far. Therefore we could specify
any type for file components (for example, real type), and the result of the program running would be the same.
But if the program contains file input-output operations then it
is important to specify the type of file components correctly, otherwise "strange" errors will occurs during
the program running. Let's model this situation in our program. For this purpose
we shall declare the variable named a as Double
and replace the Rem comment by the following statements:
For i = 1 To 3
Get f(i), , a
Put f(4), , a
Next
The given statements provide reading one component from each input file and
writing these components to the output file (in the required order). Note that we have
specified the file types incorrectly, but the program will be compiled successfully, and it
will execute without run-time errors.
The result of the program execution will be unexpected:
the output file will contain twelve components, that is, four components of each input file.
It can be explained as follows.
In our program the file elements are assumed to be real numbers (of size 8 bytes).
But in fact the input files contains integer components (of size 2 bytes).
Therefore reading and writing of one "real-valued" component leads to
reading/writing of four integer components.
Thus, we see that the errors connected with the incorrect file types are not
detected by compiler and do not usually lead to the run-time errors, but often lead to "strange" results.
After replacing the "Double" specifier by the "Integer" one in the declaration of the a variable
our program will work
quite "clearly": the output file will contain three components, that is, one initial component of
each input file.
Correct solution, its testing and browsing results
At last, let's present a correct algorithm for the File48 task:
Public Sub Solve()
Task "File48"
Dim s As String, f(1 To 4) As Integer, _
a As Integer, i As Integer, k As Integer
For i = 1 To 4
GetS s
f(i) = FreeFile
Open s For Random As f(i) Len = Len(a)
Next
For k = 1 To LOF(f(1)) \ Len(a)
For i = 1 To 3
Get f(i), , a
Put f(4), , a
Next
Next
Close
End Sub
This algorithm contains the new For statement that provides reading all components from the input files
(recall that all input files are of the same size).
After running the program we shall see the message "Right solution.
The test 1 of 5", and after 5 test runnings we shall receive the message
"The task is solved!".
Using the PT4Results tool we can browse
information about all test runnings of our program:
File48 b03/05 17:41 Acquaintance with the task.
File48 b03/05 17:45 Some required data are not input.
File48 b03/05 17:49 Wrong solution.--3
File48 b03/05 17:53 The task is solved!
String files and text files: File67, Text21
In this section we shall discuss some features of processing of
string files (i.e., binary
typed files with random access) and text files.
String files
Let's consider the File67 task as an example of task with string file processing:
File67°. A file of strings is given. The file contains dates in
the "day/month/year" format, the "day" and "month" fields contain two digits, the
"year" field contains four digits (for example, "16/04/2001"). Create two new files and
write integer values of days and months for each date from the given file to the first
and second resulting file respectively (in the same order).
If Programming taskbook is used for task solution in Visual Basic then
string files with random access are assumed to contain components of the String*80 type
(that is, the length of each file string is 80 bytes). Therefore
variables used for file input-output operations
must have String*80 type too. To find the amount of components of a string file
one should divide the file size (in bytes) by 80.
Note that files of characters are used in some tasks of Programming Taskbook.
These files are assumed to contain components of the String*1 type.
Taking into account all features mentioned above we can solve the File67 task as follows:
Option Explicit
Public Sub Solve()
Task "File67"
Dim s As String * 80, f As Integer, _
f1 As Integer, f2 As Integer, _
a As Integer, i As Integer
GetS s
f = FreeFile
Open s For Random As f Len = Len(s)
GetS s
f1 = FreeFile
Open s For Random As f1 Len = Len(a)
GetS s
f2 = FreeFile
Open s For Random As f2 Len = Len(a)
For i = 1 To LOF(f) \ 80
Get f, , s
a = Val(Mid(s, 1, 2))
Put f1, , a
a = Val(Mid(s, 4, 2))
Put f2, , a
Next
Close
End Sub
Text files
Let's consider the Text21 task as an example of task with text file processing:
Text21°. Given a text file that contains more
than three lines, remove its last three lines.
Text files, unlike files with random access, cannot be open for reading and writing
simultaneously, therefore it is necessary to use an temporary file for text file changing:
the required output data should be written to the temporary file, after that
the initial file should be removed and the temporary file name should be changed to the initial file name.
Lines of the text file are of various length, therefore it is necessary to read all lines to
determine the amount of lines in the text file.
To open a text file one should use Input, Append, or Output statements.
Arguments of the input-output statements for text files may be of String type.
Considering all features mentioned above we can solve the Text21 task as follows:
Option Explicit
Public Sub Solve()
Task "Text21"
Dim f1 As Integer, f2 As Integer, _
s1 As String, s2 As String, _
s As String, i As Integer, n As Integer
GetS s1
f1 = FreeFile
Open s1 For Input As f1
s2 = "$T21$.tmp"
f2 = FreeFile
Open s2 For Output As f2
n = 0
Do While Not EOF(f1)
Line Input #f1, s
n = n + 1
Loop
Close f1
Open s1 For Input As f1
For i = 1 To n - 3
Line Input #f1, s
Print #f2, s
Next
Close
Kill s1
Name s2 As s1
End Sub
This algorithm is inefficient because it requires to read the initial file twice:
the first time to determine the amount of file lines, the second time to create an
temporary file that will contain all lines of the initial file except three last ones.
The Text21 task can be solved using only one reading of the initial file.
In this solution we'll take into account the following consideration: a text line should be written to an
temporary file if at least three lines follow this line in the file.
This way of solution does not requires to determine the amount of the text lines.
To store strings,
which have been read from the input file but still have not been written to the temporary file, we shall
use an array of three elements.
We obtain the second, one-pass algorithm of the Text23 solution:
Option Explicit
Public Sub Solve()
Task "Text21"
Dim f1 As Integer, f2 As Integer, _
s1 As String, s2 As String, _
s(0 To 2) As String, i As Integer, n As Integer
GetS s1
f1 = FreeFile
Open s1 For Input As f1
s2 = "$T21$.tmp"
f2 = FreeFile
Open s2 For Output As f2
For i = 0 To 2
Line Input #f1, s(i)
Next
n = 0
Do While Not EOF(f1)
Print #f2, s(n)
Line Input #f1, s(n)
n = (n + 1) Mod 3
Loop
Close
Kill s1
Name s2 As s1
End Sub
|