Previous | Table of Contents | Next |
5.2.8.3. exitproc
exitproc is a procedure that is set up as an address system, which is executed on the compiler automatically upon completion of the program, no matter what kind of error may happen, but before a runtime error code message. A common application for this feature is the addition of runtime error-description code, or diagnostic logging features for a program, or clean-up functions for working with files. Listing 5.33 shows an example.
Listing 5.33. An example of the use of exitproc.
program fig33; const a: integer = 3; b: integer = 0; var exitsave: procedure; {$F+} { must be far } procedure myexit; {must be parameterless} begin writeln; writeln(Exit.); if exitcode <> 0 then { exitcode used to hold run-time error number} writeln(There was a problem.) else writeln(Successful termination.); end; {$F-} begin @exitsave := exitproc; { saving the original exit procedure } exitproc := @myexit; { set to new exit procedure} writeln(Hello!); { force a division by zero RUNTIME error } writeln(Divide by Zero: , a/b); exitproc := @exitsave; { set the original exit procedure back } end.
In Turbo Pascal, everything that is read from or written to is considered to be a file. This includes the keyboard and the screen as well. Turbo Pascals behavior of having default files has somewhat shielded the reader from working with files for the time being. In the initialization code for a Pascal executable, a default file of input is assigned to the keyboard, and a default file of output is assigned to the monitor, as file variables input and output. In this sense, a description of what file to write to has not been needed. But for other files, file descriptions have to be specified.
Remember, in the descriptions in this section, there are no such things as different types of files. All files are the same, though the way they are accessed is different, which is the true meaning of a file type. These different methods of accessing files are described in this section. Among those are ASCII text files, typed and untyped binary files, and the printer.
Note, in addition, that most of the source code examples in this section use a file called INTDATA.TXT, which either may be edited or generated by a file named TESTTEXT.EXE. In the examples, the generated version is used; it is an edited ASCII text file, with a number of lines between 300 and 399, and a number of integers (as text) between 0 and 24 per line of text.
5.2.9.1. File Access in General
Any file, to be usable in Turbo Pascal, must have two procedure statements to be able to access it. The first is an assign statement, assigning a file variable to a specific filename (as a string). The specific filename may or may not have an accompanying path. After this call, the file is always referred to by the file variable. File variables, when designated in the var section of a program, procedure, or function, are referred to depending on the way the file is accessed.
The next is either a reset(), rewrite(), or append() call, with the file variable as the parameter. These procedure calls open the file in a particular way to be accessed. reset() opens a file in a read-only mode for text files and a read/write mode for other types of files at the beginning position of the file. rewrite() opens any type of file in a write-only mode, in the process creating the file on the drive (beware if the file already exists, that file will be gone) with that name. append() opens a text file and allows writes to it at the end of the current text stored in the file. Note the limitations in DOS of the maximum number of files that may be opened; this limitation is stated in the CONFIG.SYS of the particular system as files=XX.
The final procedure call, which must be used to close a file after it is used, is close(<file variable>). This procedure flushes the disk caches and buffers contents related to that file and releases the file handle.
When a file is accessed and associated with a file variable, reading and writing of the file must generally occur. This is accomplished by adding the file variable name as the first parameter of a read or write call. Note in using the read and write procedures that the file variable must always be included as the first parameter.
Previous | Table of Contents | Next |