1.2.38 $I or $IOCHECKS : Input/Output checking

The {$I-} or {$IOCHECKS OFF} directive tells the compiler not to generate input/output checking code in the program. By default, the compiler generates I/O checking code. This behavior can be controlled globally with the -Ci switch.

When compiling using the -Ci compiler switch, the Free Pascal compiler inserts input/output checking code after every input/output call in the code. If an error occurred during input or output, then a run-time error will be generated. This switch can also be used to avoid this behavior.

If no I/O checking code is generated, to check if something went wrong, the IOResult function can be used to see if everything went without problems.

Conversely, {$I+} will turn error-checking back on, until another directive is encountered which turns it off again.

Remark When an I/O error occurs and I/O-checking code generation is off, all subsequent I/O operations are ignored until the IO error status code is cleared.

When the IOResult function from the system unit is called, it returns the current IO status code and resets it.

The effect of this is that IOResult must be checked after each I/O operation for subsequent I/O operations to succeed.

The most common use for this switch is to check if the opening of a file went without problems, as in the following piece of code:

assign (f,’file.txt’);  
{$I-}  
rewrite (f);  
{$I+}  
if IOResult<>0 then  
  begin  
  Writeln (’Error opening file: "file.txt"’);  
  exit  
  end;

See the IOResult function explanation in Reference Guide for a detailed description of the possible errors that can occur when using input/output checking.