Name

IOResult Function

Syntax

function IOResult: Integer;

Description

Delphi has two ways to report an I/O error: runtime errors or the IOResult function. By default, Delphi reports I/O errors as runtime errors, and the SysUtils unit maps runtime errors to exceptions. If you use the $I- or $IOChecks Off directives to turn off I/O runtime errors, Delphi returns the status of input and output operations in the IOResult function. It is the programmer’s responsibility to call IOResult to test the success or failure of each I/O procedure or function call.

IOResult returns zero for success or an error code for failure. The error code might be a Windows error code, or one of the following Delphi error codes.

Error Number

Description

100

Read past end of file.

101

Disk is full.

102

AssignFile has not yet been called.

103

The file is closed.

104

File not open for input.

105

File not open for output.

106

Incorrectly formatted input for Read.

After you call IOResult, the I/O result code is reset to zero. IOResult is a real function.

Tips and Tricks

  • Each thread keeps its own I/O result code, so be sure to call IOResult in the context of the correct thread.

  • It is the programmer’s responsibility to call IOResult after each I/O routine. If you don’t check IOResult, a subsequent I/O call can overwrite the old error code with a new one.

  • The SysUtils unit maps I/O errors to the EInOutError exception. This exception class maps only a few I/O error codes to strings. In a real application, ...

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.