Ad Space — Top Banner

EInOutError

Delphi Programming Language

Severity: Moderate

What Does This Error Mean?

EInOutError is raised when a file input/output operation fails. This covers reading from or writing to files using Delphi's classic Pascal file routines (Reset, Rewrite, Read, Write, Assign, Close). Common causes include the file not existing, not having permission to access it, or the disk being full.

Affected Models

  • All Delphi versions
  • Classic Pascal I/O routines
  • I/O checking must be enabled

Common Causes

  • The file does not exist at the path specified when trying to open it for reading
  • The program does not have permission to read or write the file
  • The disk is full and there is no space to write new data
  • Trying to read past the end of the file
  • The file is already open by another process and cannot be accessed

How to Fix It

  1. Check that the file exists before trying to open it. Use FileExists('path') which returns True or False.

    For directories, use DirectoryExists(). Both are in the SysUtils unit.

  2. Make sure your application has permission to access the file. Check the file's permissions and whether you need administrator rights to read or write it.

    Files in C:\Windows, C:\Program Files, and similar system directories often require admin rights.

  3. When working with I/O check mode ({$I+}, which is the default), always wrap file operations in try/except EInOutError blocks to handle errors gracefully.

    Example: try AssignFile(F, 'data.txt'); Reset(F); except on E: EInOutError do ShowMessage('Error: ' + E.Message); end;

  4. For new code, use TFileStream and TStreamReader/TStreamWriter instead of classic Pascal file routines. They integrate better with Delphi's exception handling and are more versatile.

    Example: Stream := TFileStream.Create('data.txt', fmOpenRead or fmShareDenyWrite);

  5. After an error, make sure to close the file if it was opened. Use try/finally to ensure CloseFile (or Close) is always called, even when an exception occurs.

    Leaving files open causes file handle leaks and can prevent other programs from accessing the file.

When to Call a Professional

EInOutError is always something you can fix yourself. The ErrorCode property of the exception gives you the specific OS error number. For modern Delphi development, prefer TFileStream and TStreamReader over classic Pascal file routines — they raise more specific exceptions.

Frequently Asked Questions

What is the ErrorCode property of EInOutError?

It is the Windows or OS error code number that caused the failure. Common codes: 2 means file not found, 5 means access denied, 32 means file is in use by another process, 112 means disk is full. You can look up the code with SysErrorMessage(E.ErrorCode) to get a human-readable description.

What is the difference between {$I+} and {$I-} modes?

With {$I+} (the default), Delphi automatically raises EInOutError when an I/O operation fails. With {$I-}, errors are not raised automatically — you check IOResult after each operation. Modern Delphi code should use {$I+} and handle errors with try/except, which is cleaner than checking IOResult manually.

Should I use classic Pascal file routines or TFileStream for new code?

Use TFileStream, TStreamReader, and TStreamWriter for new code. They are more flexible, support Unicode properly, and integrate better with modern Delphi features. Classic file routines (AssignFile, Reset, Rewrite) are from the original Turbo Pascal days and are kept for backward compatibility.