Search code examples
pascal

Virtual Pascal RunTime Error (or no output) if including Printer unit?


I'm using Virtual Pascal on Windows and have a weird problem whereby if an attempt to Reset() any file that doesn't exist causes the WriteLn() to CRT fail. Example:

{$I+}
program TestIt;

uses Printer; { attempts to open LPT1 as Lst but LPT1 doesn't exist on system }

begin
  { Generates a RunTime Error with $I+ and no output with $I- }
  writeln('Test It');
end.

This also fails:

program TestIt;

var
  SomeFile : text;
begin
{$I-} 
  Assign(SomeFile, 'a:\filepath_not_exist');
  { Without $I- the Reset generates a RunTime Error as expected }
  Reset(SomeFile);
{$I+}
  { Generates a RunTime Error with $I+ and no output with $I- }
  writeln('Test It');
end.

This works as expected:

{$I+}
program TestIt;

begin
  writeln('Test It');
end.

Any ideas why this may be happening and how to fix it? Is the source available for WriteLn() or Assign() ? I'm able to change the Printer unit to not Reset() to work around it until needing to get printing working but I don't think failure to open a file should cause the screen output to stop working.


Solution

  • Probably the error state is kept in some variable (which is queried by ioresult) and might cause an error to be raised in the next I/O function. If you don't do automatic error checking ($I+) you must call ioresult after every operation.

    Such implementation is not ideal (if errors from the opening using "somefile" spill into a writeln to a second(output) file as happens in the second example), but it happens, specially in older compilers, and specially on targets that are less dos-like.

    Also, not existing drive errors are much harder to cache than non existing files. Keep those cases fundamentally apart.

    You can test this theory by trying to reset state by querying ioresult just before the action in the hope it resets the state.

    Afaik VP is dead for almost a decade now. What you see (read: download) is what you get.