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: 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.
Some of the standard I/O routines
that work on text files don’t need to have a file variable explicitly given as a
parameter. If the file parameter is omitted, Input or Output is
assumed by default, depending on whether the procedure or function is input- or
output-oriented. For example, Read(X) corresponds to Read(Input, X) and
Write(X) corresponds to Write(Output, X).
If you do specify a file when calling one of the input or output routines that work on text files, the file must be associated with an external file using AssignFile, and opened using Reset, Rewrite, or Append. An exception is raised if you pass a file that was opened with Reset to an output-oriented procedure or function. An exception is also raised if you pass a file that was opened with Rewrite or Append to an input-oriented procedure or function.
Standard routines and I/O: Overview
本节概述使用标准文件类型Text类型变量时的输入输出(I/O)。
当一个文本文件被打开时,相应的外部文件以特别的方式被解释:它被认为表示一个字符序列,这些字符被格式化到若干行,每一行都以行结束标记end-of-line(回车字符,如果可能,还跟随一个换行字符)终止。标准文本文件Text类型与字符类型文件file of Char是不同的。
文本文件有特殊的Read和Write形式来读和写那些非Char类型的值。这些值被自动转化到相应的字符描述的形式。例如,Read(F, I),这里的 I 是一个Integer类型的变量,该操作将读出一个数字序列,并将该序列解释为十进制整数,然后存储到变量 I 中。
Object Pascal预定义了两个标准的文本文件变量,Input和Output。标准文件变量Input是一个只读文件,它与操作系统的标准输入设备(典型地如键盘)相关联。标准文件变量Output是一个只写文件,它与操作系统的标准输出设备(典型地如显示器)相关联。在应用程序开始执行之前,Input和Output被自动打开,好象下列语句被执行一样:
AssignFile(Input, '');
Reset(Input);
AssignFile(Output, '');
Rewrite(Output);
注意: 面向文本的输入/输出(I/O)仅在控制台应用程序中是可用的。控制台应用程序是指,在Project Options对话框中Linker页上选中“Generate console application”选项(Delphi6中)时编译产生的应用程序,或者指在命令行方式下使用-cc编译指示编译产生的应用程序。在图形用户界面GUI(非控制台)应用程序中,任何试图用Input或Output进行读或写的操作都将导致I/O错误。
一些工作于文本文件的标准I/O例程不需要明确地给出文件变量作为参数。如果文件参数被省略,那么Input或Output被假定为缺省的,具体选择哪一个取决于过程或函数是面向输入还是面向输出的。例如,Read(X)相当于Read(Input, X),而Write(X)相当于Write(Output, X)。
如果要调用某个工作于文本文件的输入或输出例程来处理指定的文件,那么必须用AssignFile将文件与外部文件相关联,并且用Reset、Rewrite或Append打开。对于用Reset过程打开的文件变量,如果将其传递给一个面向输出的过程或函数,那么将引发异常。对于用Rewrite过程或Append过程打开的文件,如果将其传递给一个面向输入的过程或函数,那么也将引发异常。