Quick start manual

Standard routines and I/O
8-3
File input and output
When a program completes processing a file, the file must be closed using the
standard procedure CloseFile. After a file is closed, its associated external file is
updated. The file variable can then be associated with another external file.
By default, all calls to standard I/O procedures and functions are automatically
checked for errors, and if an error occurs an exception is raised (or the program is
terminated if exception handling is not enabled). This automatic checking can be
turned on and off using the {$I+} and {$I–} compiler directives. When I/O checking is
off—that is, when a procedure or function call is compiled in the {$I–} state—an I/O
error doesn’t cause an exception to be raised; to check the result of an I/O operation,
you must call the standard function IOResult instead.
You must call the IOResult function to clear an error, even if you aren’t interested in
the error. If you don’t clear an error and {$I+} is the current state, the next I/O
function call will fail with the lingering IOResult error.
Text files
This section summarizes I/O using file variables of the standard type Text.
When a text file is opened, the external file is interpreted in a special way: It is
considered to represent a sequence of characters formatted into lines, where each line
is terminated by an end-of-line marker (a carriage-return character, possibly
followed by a linefeed character). The type Text is distinct from the type file of Char.
For text files, there are special forms of Read and Write that let you read and write
values that are not of type Char. Such values are automatically translated to and from
their character representation. For example, Read(F, I), where I is a type Integer
variable, reads a sequence of digits, interprets that sequence as a decimal integer, and
stores it in I.
There are two standard text file variables, Input and Output. The standard file
variable Input is a read-only file associated with the operating system’s standard
input (typically, the keyboard). The standard file variable Output is a write-only file
associated with the operating system’s standard output (typically, the display).
Before an application begins executing, Input and Output are automatically opened,
as if the following statements were executed:
AssignFile(Input, '');
Reset(Input);
AssignFile(Output, '');
Rewrite(Output);
Note
For Win32 applications, text-oriented I/O is available only in console applications—
that is, applications compiled with the “Generate console application” option
checked on the Linker page of the Project Options dialog box or with the -cc
command-line compiler option. In a GUI (non-console) application, any attempt to
read or write using Input or Output will produce an I/O error.