[comp.lang.pascal] Statusof a msdos file after dos error

kushmer@bnlux0.bnl.gov (christopher kushmerick) (12/06/90)

After a file has been opened (Via assign, reset/rewrite) what is its
status following a disk operation that results in dos reporting an io error?


Here is the exact situation:

turbo pascal 5.0

I open a file. the reset/rewite occurs with success.

Now I do an operation, and it fails, perhas, for example, because the
user took the diskette out of the drive. What is the status of the file?
Do I have to close it? Can I close it? should I close it?

It should be obvious that I am compiling this with {$I-} set so that I may 
process my own io errors.



-- 
Chris Kushmerick
kushmer@bnlux0.bnl.gov    <===Try this one first
kushmerick@pofvax.sunysb.edu 

bank@lea.ncsu.edu (Dave The DM) (12/07/90)

In article <2350@bnlux0.bnl.gov> kushmer@bnlux0.bnl.gov (christopher kushmerick) writes:
>After a file has been opened (Via assign, reset/rewrite) what is its
>status following a disk operation that results in dos reporting an io error?
>
>
>Here is the exact situation:
>
>turbo pascal 5.0
>
>I open a file. the reset/rewite occurs with success.
>
>Now I do an operation, and it fails, perhas, for example, because the
>user took the diskette out of the drive. What is the status of the file?
>Do I have to close it? Can I close it? should I close it?
>
>It should be obvious that I am compiling this with {$I-} set so that I may 
>process my own io errors.
>
>
>
>-- 
>Chris Kushmerick
>kushmer@bnlux0.bnl.gov    <===Try this one first
>kushmerick@pofvax.sunysb.edu 

    1) What do you mean by "status"?? The location of the file pointer
       is (in most situations) undefined. The file is probably still
       there. I'm not sure what you're looking for here.

    2) Implicit in your question is "Is the file still open?"  Yes, it
       is (well, in all the PC language implementations I have worked
       in - TP 2.0 thru 5.0, MS C 5.1 thru 6.00a, etc). Do you "have"
       to close it?? I dunno. What do you want to do with it. You may
       want to retry your operation. Or you may want to close it, then
       reopen, then retry. You don't HAVE to close it, altho in some
       cases this can be a good idea (depends on the nature of the
       I/O error).

    3) Can you close it?? Again, depends on the exact I/O error. If
       the problem came about because some lobotomized pea-brain
       opened a diskette drive door, then I daresay that a close
       operation won't have much success. If it was just a system burp,
       then you may not have any problem.
 
    4) Should you close it?? See 2) above. It all depends on exactly
       what the problem was and what you are doing with the file.
       Depending on what you're using for, closing the file can range
       from a great idea (perhaps its your master database file) to
       a so-so idea (its your temporary data file). Its rarely a bad
       idea.

    File ops are always a pain. The DOS 3.x critical error handler lists
gobs and gobs of different error codes for file operations (which is
a good thing - I HATE not knowing >>exactly<< what the system thinks
went wrong.
           )
    
    Anyway, hope this helps.

Dave the DM
bank@lea.csc.ncsu.edu

John G. Spragge <SPRAGGEJ@QUCDN.QueensU.CA> (12/07/90)

On closing the file: If the actual error was disk drive open, I
would hesitate. I suspect that if the user had gone so far as to switch
the disks, a close might destroy the new disk's directory (and/or FAT).

In any case, your action should ideally depend on the exact nature of the
I/O error, and to some extent, on the suspected nature of the user.

disclaimer: Queen's University merely supplies me with computer services, and
            they are responsible for neither my opinions or my ignorance.

John G. Spragge

peters@yang.earlham.edu (12/14/90)

In article <2350@bnlux0.bnl.gov>, kushmer@bnlux0.bnl.gov (christopher kushmerick) write> After a file has been opened (Via assign, reset/rewrite) what is its 
> Here is the exact situation:
> 
> turbo pascal 5.0
> 
> I open a file. the reset/rewite occurs with success.
> 
> Now I do an operation, and it fails, perhas, for example, because the
> user took the diskette out of the drive. What is the status of the file?
> Do I have to close it? Can I close it? should I close it?
> 
> Chris Kushmerick
> kushmer@bnlux0.bnl.gov    <===Try this one first
> kushmerick@pofvax.sunysb.edu 

The answer depends on whether you opened the file with reset or rewrite.
If you opened it with reset, to read it, then a failed operation will leave
the file uncorrupted (unless the failure is due to a dog eating the physical
disk, etc.).  If you opened it with rewrite or append, to write to it, then
the chances are very good that any write/ln's performed between the opening
of the file and the failed operation did not make it to the disk.  In TP,
closing a file is the way to insure that writes are flushed from the buffer
and sent to the disk.  Verify this by opening a file with rewrite, sending
some text to the file with write/ln, and watching the light on the disk drive;
it will not glow until the close command is processed.

Peter Suber.  Depts. of Philosophy, Computer Science.  Earlham College.
Richmond, Indiana 47374.  317-983-1214.  peters@earlham.bitnet.

bobb@vice.ICO.TEK.COM (Bob Beauchaine) (12/15/90)

In article <6032@yang.earlham.edu> peters@yang.earlham.edu writes:
>
>The answer depends on whether you opened the file with reset or rewrite.
>Verify this by opening a file with rewrite, sending
>some text to the file with write/ln, and watching the light on the disk drive;
>it will not glow until the close command is processed.
>

  Close, but not quite true.  There are three cases that will flush a text 
  file in Turbo Pascal:

  1.  As you stated, when the file is closed.
  2.  If you call the flush(var f: text) procedure
  3.  When the internal file buffer fills, which happens every 128 bytes
      if you have not declared your own (larger) text file buffer.

  Any of the above three actions will send all pending data to the file.
  Note that this does *not* guarantee that the operating system will
  know about the data; the file size information and allocation chain in 
  the FAT are not updated until the file is closed.

  Bob Beauchaine
  bobb@vice.ICO.TEK.COM