Delphi IO Error 103


Similarly to [this], the multithreading in a project has also caused the strange error, which is IO Error 103. It does not happen every time but randomly. I googled a bit and found this poston stackoverflow.

And I started to look into the Delphi code which has the File opening functions using Reset, AssignFile etc. Before, the following function will return the size of a given file.

function getFileSizeInBytes(const fn: string): integer;
var
  f: File of byte;
begin
  Result := -1;
  if (FileExists(fn)) then
  begin
    try
      AssignFile(f, fn);
      Reset(f);
      Result := FileSize(f);
    finally
      CloseFile(f);
    end;
  end;
end;

The problem occurred most readily when one logging operation swiftly followed another. The second operation would fail for the above reason. To solve the IO 103 error, using the IO directive {$I-} to suppress the exceptions and {$I+} to turn it on. The variable IOResult indicates that the IO operation is successful if it is zero. For example, the above code could be improved using the IOResult.

function getFileSizeInBytes(const fn: string): integer;
var
  f: File of byte;
begin
  Result := -1;
  if (FileExists(fn)) then
  begin
    try
      {$I-}
      AssignFile(f, fn);
      Reset(f);
      {$I+}
      if (IOResult = 0) then
      begin
        Result := FileSize(f);
      end
      else
      begin
        Result := 0;
      end;
    finally
      CloseFile(f);
    end;
  end;
end;

The function returns zero if there is an exception in reseting the file. If not, the IO Error 103 will be thrown out. Adding the IO code in the try … except … may suppress the 103 error. {$I+} is identical to {$IOCHECKS ON} and {$I-} is the same as {$IOCHECKS OFF}.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
331 words
Last Post: Using DocStrings in Python
Next Post: Dropbox Provides Light-weight FTP space

The Permanent URL is: Delphi IO Error 103

Leave a Reply