Additional Returnvalues with File-Commands

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Additional Returnvalues with File-Commands

Post by freedimension »

According to GPIs post in the German Board:

It seems that all the WriteXXX()-Functions don't return an errorcode or something like that. Therefore you can't react adequately to failures (for example a full disc).

Additionaly you could add ReturnValues to the CloseFile()-Procedure

0 - no Error
1 - Couldn't write - disc full
2 - File writeprotected
3 - etc.

This way you could also check the process of file creation and writing at the end.
User avatar
IceSoft
Addict
Addict
Posts: 1683
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Post by IceSoft »

@Fred
I belive "a missing return status" is more a BUG as a wish.
Please move it to the BUG list. Thanks!
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Additional Returnvalues with File-Commands

Post by PB »

> It seems that all the WriteXXX()-Functions don't return an errorcode

That's because OpenFile and CreateFile do the checking... they must return
non-zero before you can start writing with WriteXXX().

> Therefore you can't react adequately to failures (for example a full disc)

"Disk full" is the only error that can occur after an initial successful check.
The other ones you specified (write-protected, etc) get checked beforehand.

Here's an example of a way to check if WriteStringN() worked. All it does is
check the filesize before and after each write, and if the size is the same,
then obviously the write failed:

Code: Select all

Procedure CheckWriteStringN(string$)
  a=Lof() : WriteStringN(string$)
  If Lof()<>a : b=#TRUE : EndIf
  ProcedureReturn b
EndProcedure

If CreateFile(0,"c:\test.txt")<>0
  For r=1 To 10
    If CheckWriteStringN(Str(r))=#FALSE
      Debug "Write failed!"
      Break ; Exit For/Next loop.
    EndIf
  Next
  CloseFile(0)
EndIf
Post Reply