Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

file IO error

ragnarok59ragnarok59 Posts: 2Member
I am using Turbo Pascal 5.5 and have the following code as part of a program

assign(fileIn, comFile);
reset(fileIn);
read(fileIn, flag);
close(fileIn);

However, the file is also used by another program running concurrently and often I get "Runtime error 5: file access denied." Is there a way for me to check if the assign or reset statement failed and try again until it succeeds (similar to fstream.good() in c++ )? In case it helps, the goal is to have two programs communicate with each other through an intermediate file. Thank you.

Comments

  • zibadianzibadian Posts: 6,349Member
    : I am using Turbo Pascal 5.5 and have the following code as part of a program
    :
    : assign(fileIn, comFile);
    : reset(fileIn);
    : read(fileIn, flag);
    : close(fileIn);
    :
    : However, the file is also used by another program running concurrently and often I get "Runtime error 5: file access denied." Is there a way for me to check if the assign or reset statement failed and try again until it succeeds (similar to fstream.good() in c++ )? In case it helps, the goal is to have two programs communicate with each other through an intermediate file. Thank you.
    :
    Here is a code snippit, which might work. I never used TP5.5 so I'm not sure, in TP7 it will work.
    [code]
    Assign(...)
    repeat
    {$I-} { turn off the i/o error checking }
    reset(f);
    {$I+} { turn it back on }
    until IOResult = 0; { repeat opening until no error }
    ...
    [/code]
    It could also be that you need to place the Assign() into the loop.
  • ManningManning Posts: 1,621Member
    : : I am using Turbo Pascal 5.5 and have the following code as part of a program
    : :
    : : assign(fileIn, comFile);
    : : reset(fileIn);
    : : read(fileIn, flag);
    : : close(fileIn);
    : :
    : : However, the file is also used by another program running concurrently and often I get "Runtime error 5: file access denied." Is there a way for me to check if the assign or reset statement failed and try again until it succeeds (similar to fstream.good() in c++ )? In case it helps, the goal is to have two programs communicate with each other through an intermediate file. Thank you.
    : :
    : Here is a code snippit, which might work. I never used TP5.5 so I'm not sure, in TP7 it will work.
    : [code]
    : Assign(...)
    : repeat
    : {$I-} { turn off the i/o error checking }
    : reset(f);
    : {$I+} { turn it back on }
    : until IOResult = 0; { repeat opening until no error }
    : ...
    : [/code]
    : It could also be that you need to place the Assign() into the loop.


    Yeah that should work just fine in TP5.5. You might want to expand on "until IOResult = xx" though, for example if it throws an IOResult indicating that the file isnt found you could sit in an infinite loop.

    Some people try to handle each IOResult value and take action depending on that, but from looking at other people's code I think the much more common (although not necessarily better) approach is to use "until IOResult = 0 or 5 seconds have passed".
  • ragnarok59ragnarok59 Posts: 2Member
    Thank you very much to both of you. I will try this out as soon as possbile.
Sign In or Register to comment.