The table
below lists input and output routines.
|
Procedure or function |
Description |
|
Append |
Opens an existing text file for appending. |
|
AssignFile |
Assigns the name of an external file to a file variable. |
|
BlockRead |
Reads one or more records from an untyped file. |
|
BlockWrite |
Writes one or more records into an untyped file. |
|
ChDir |
Changes the current directory. |
|
CloseFile |
Closes an open file. |
|
Eof |
Returns the end-of-file status of a file. |
|
Eoln |
Returns the end-of-line status of a text file. |
|
Erase |
Erases an external file. |
|
FilePos |
Returns the current file position of a typed or untyped file. |
|
FileSize |
Returns the current size of a file; not used for text files. |
|
Flush |
Flushes the buffer of an output text file. |
|
GetDir |
Returns the current directory of a specified drive. |
|
IOResult |
Returns an integer value that is the status of the last I/O function performed. |
|
MkDir |
Creates a subdirectory. |
|
Read |
Reads one or more values from a file into one or more variables. |
|
Readln |
Does what Read does and then skips to beginning of next line in the text file. |
|
Rename |
Renames an external file. |
|
Reset |
Opens an existing file. |
|
Rewrite |
Creates and opens a new file. |
|
RmDir |
Removes an empty subdirectory. |
|
Seek |
Moves the current position of a typed or untyped file to a specified component. Not used with text files. |
|
SeekEof |
Returns the end-of-file status of a text file. |
|
SeekEoln |
Returns the end-of-line status of a text file. |
|
SetTextBuf |
Assigns an I/O buffer to a text file. |
|
Truncate |
Truncates a typed or untyped file at the current file position. |
|
Write |
Writes one or more values to a file. |
|
Write |
Writes one or more values to a file. |
|
Writeln |
Does the same as Write, and then writes an end-of-line marker to the text file. |
A file
variable is any variable whose type is a file type. There are three classes of
file: typed, text, and untyped. The syntax for declaring
file types is given in File
types.
Before a
file variable can be used, it must be associated with an external file through
a call to the AssignFile procedure. An external file is typically a
named disk file, but it can also be a device, such as the keyboard or the
display. The external file stores the information written to the file or
supplies the information read from the file.
Once the
association with an external file is established, the file variable must be
opened to prepare it for input or output. An existing file can be opened via
the Reset procedure, and a new file can be created and opened via the Rewrite
procedure. Text files opened with Reset are read-only and text files
opened with Rewrite and Append are write-only. Typed files and
untyped files always allow both reading and writing regardless of whether they
were opened with Reset or Rewrite.
Every
file is a linear sequence of components, each of which has the component type
(or record type) of the file. The components are numbered starting with zero.
Files are
normally accessed sequentially. That is, when a component is read using the
standard procedure Read or written using the standard procedure Write,
the current file position moves to the next numerically ordered file component.
Typed files and untyped files can also be accessed randomly through the
standard procedure Seek, which moves the current file position to a
specified component. The standard functions FilePos and FileSize
can be used to determine the current file position and the current file size.
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.
下表列出了输入和输出例程:
|
Procedure or function |
Description |
|
Append |
打开存在的文本文件(用于对文件追加文本)。 |
|
AssignFile |
指定外部文件的文件名到文件变量。 |
|
BlockRead |
从无类型文件读出一个或多个记录。 |
|
BlockWrite |
向无类型文件写入一个或多个记录。 |
|
ChDir |
改变当前目录。 |
|
CloseFile |
关闭打开的文件。 |
|
Eof |
返回文件的文件结束(end-of-file)状态。 |
|
Eoln |
返回文本文件的行结束(end-of-line)状态。 |
|
Erase |
删除外部文件。 |
|
FilePos |
返回类型文件或无类型文件的当前位置。 |
|
FileSize |
返回文件的当前尺寸;不适用于文本文件。 |
|
Flush |
刷新输出文本文件的缓冲区。 |
|
GetDir |
返回指定驱动器的当前目录。 |
|
IOResult |
返回表示最后一次I/O函数执行状态的整数值。 |
|
MkDir |
创建子目录。 |
|
Read |
从文件读出一个或多个值到一个或多个变量。 |
|
Readln |
在文本文件中执行Read操作并跳至下一行行首。 |
|
Rename |
对外部文件重命名。 |
|
Reset |
打开存在的文件。 |
|
Rewrite |
创建并打开新文件。 |
|
RmDir |
删除空的子目录。 |
|
Seek |
从类型文件或无类型文件的当前位置移动到指定的分量;不适用于文本文件。 |
|
SeekEof |
返回文本文件的文件结束(end-of-file)状态。 |
|
SeekEoln |
返回文本文件的行结束(end-of-line)状态。 |
|
SetTextBuf |
指定输入/输出(I/O)缓冲区到文本文件。 |
|
Truncate |
在类型文件或无类型文件的当前位置截断文件。 |
|
Write |
向文件写入一个或多个值。 |
|
Writeln |
在文本文件中执行Write操作然后写入一个行结束(end-of-line)标记。 |
文件变量就是其类型是文件类型的任何变量。在Object Pascal中,文件类型有三种:类型文件(typed)、文本文件(text)和无类型文件(untyped)。文件类型的声明见文件类型。
在文件变量能被使用之前,必需通过调用AssignFile过程将其与外部文件相关联。一个外部文件通常是一个命名过的磁盘文件,但也可以是一个设备,如键盘或显示器。外部文件存储写入的信息到文件或提供从文件读出的信息。
一旦与外部文件的关联被确定,文件变量就必需被打开并为输入或输出做准备。存在的文件可以由Reset过程打开,新文件可以由Rewrite过程创建并打开。用Reset打开的文本文件是只读的(read-only),用Rewrite或Append打开的文本文件是只写的(write-only)。类型文件和无类型文件都允许读和写,而不管它们是由Reset还是由Rewrite打开的。
每个文件都是分量的线性序列,并且都具有文件的分量类型(或记录类型)。分量从零开始编号。
文件的访问通常是顺序的。也就是说,当用标准过程Read读一个分量或用标准过程Write写一个分量时,当前的文件位置将移动到下一个顺序编号的文件分量。也可以通过标准过程Seek对类型文件和无类型文件进行随机访问,Seek过程移动当前的文件位置到指定的分量。标准函数FilePos和FileSize可以分别用于确定当前的文件位置和当前的文件尺寸。
当一段程序完成对某个文件的处理时,必须使用标准过程CloseFile关闭该文件。文件被关闭后,它与外部文件的关联也被更新。此时,文件变量可以被关联其他的外部文件。
缺省情况下,所有对标准I/O过程和函数的调用都会自动检查错误,并且,如果一个错误发生,那么相应的异常被引发(或者,如果异常处理程序未被激活,那么程序被终止)。这样的自动检查可以用编译指示
{$I+} 和
{$I-} 来打开和关闭。I/O检查被关闭,即过程或函数调用在 {$I-} 状态下被编译时,I/O错误不会导致异常被引发;此时要检查I/O操作的结果,则必须调用标准函数IOResult来替代。
必须调用IOResult函数来清除I/O错误,即使对该错误没有兴趣。如果没有清除错误并且
{$I+} 是当前状态,那么下一个I/O函数调用将由于遗留的IOResult错误而失败。
编者注
原文中有如下陈述:
一旦与外部文件的关联被确定,文件变量就必需被打开并为输入或输出做准备。
经编者考证,此陈述不够准确。例如,
const
FileName1 = 'c:\test1.txt';
FileName2 = 'c:\test2.txt';
var
F: TextFile;
S: string;
begin
AssignFile(F, FileName1);
AssignFile(F, FileName2);//因为F没有被打开,所以可以重新指定外部文件关联。
Rewrite(F);
S := 'Hello';
Writeln(F, S);
CloseFile(F);
AssignFile(F, FileName1);
CloseFile(F);//因为F没有被打开,所以运行时此语句将引发异常。
end;
由此可见,对于文件变量F,在对其进行AssignFile操作之后,如果没有进行任何打开操作(Reset、Rewrite、Append),那么就不能对其进行访问(读、写),更不能对其执行关闭文件(CloseFile)的操作,但可以直接重新指定该文件变量与其他外部文件相关联。而如果既执行了AssignFile操作,又执行了打开操作(不一定进行访问),那么在关闭该文件变量之前不能对其进行新的AssignFile操作。例如,
const
FileName1 = 'c:\test1.txt';
FileName2 = 'c:\test2.txt';
var
F: TextFile;
S: string;
begin
AssignFile(F, FileName1);
AssignFile(F, FileName2);//因为F没有被打开,所以可以重新指定外部文件关联。
Rewrite(F);
AssignFile(F, FileName1);//因为F已经被打开,所以此操作将引发异常。
end;