IOResult Function
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 |
|
103 |
The file is closed. |
104 |
File not open for input. |
105 |
File not open for output. |
106 |
Incorrectly formatted input for |
After you call IOResult
, the I/O result code is
reset to zero. IOResult
is a real function.
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, you
should catch EInOutError
and map the error code
yourself. For example, you might use the following as an application
OnException
handler:
procedure TForm1.AppEventsException(Sender: TObject; E: Exception); resourcestring sEOF = 'Attempt to read past end of file'; sNotAssigned = 'File not assigned'; sNotOpen = 'File not open'; sNotRead = 'File not open for input'; sNotWrite = 'File not open for output'; sBadRead = 'Input format error'; begin if E is EInOutError then case EInOutError(E).ErrorCode of 100: E.Message := sEOF; 101: E.Message := SysErrorMessage(Error_Disk_Full); 102: E.Message := sNotAssigned; 103: E.Message := sNotOpen; 104: E.Message := sNotRead; 105: E.Message := sNotWrite; 106: E.Message := SBadRead; else E.Message := SysErrorMessage(EInOutError(E).ErrorCode); end; Application.ShowException(E); end;
// Create a directory. If the directory already exists, do nothing. procedure CreateDir(const Dir: string); type TErrorProc = procedure(Error: integer; Addr: Pointer); var Error: Integer; begin {$IOChecks Off} MkDir(Dir); {$IOChecks On} Error := IOResult; if (Error <> 0) and (Error <> Error_Already_Exists) then // Some error other than that the directory already exists. TErrorProc(ErrorProc)(Error, @CreateDir); end;
Append Procedure, AssignFile Procedure, BlockRead Procedure, BlockWrite Procedure, ChDir Procedure, CloseFile Procedure, Eof Function, Eoln Function, Erase Procedure, FilePos Function, FileSize Function, Flush Procedure, MkDir Procedure, Read Procedure, ReadLn Procedure, Rename Procedure, Reset Procedure, Rewrite Procedure, RmDir Procedure, Seek Procedure, SeekEof Function, SeekEoln Function, SetTextBuf Procedure, Truncate Procedure, Write Procedure, WriteLn Procedure, $I Compiler Directive, $IOChecks Compiler Directive |