Page 1 of 1

File Library: add GetLastFileError(file) command

Posted: Sat Feb 15, 2020 4:21 pm
by Tristano
I hope these haven't been already asked for (a quick search didn't reveal any feature requests for these).

It would be useful to have a new command to check whether the last file I/O operation on a give file ended in success or failure. Something like GetLastFileError(file) (simila to the C Standard Library ferror() function.

This new command is needed especially when reading data types from file (e.g. ReadByte(), ReadDouble(), etc.), since errors are reported by returning 0, which could also be the read value, so there's no way to know if it was an error except by checking if the file position has moved forward or not — see @Sciro's workaround in the following post:

viewtopic.php?p=534874#p534874

Code: Select all

If CreateFile(0, GetTemporaryDirectory() + "test")
  For i = 1 To 10
    WriteByte(0, i)
  Next
  FileSeek(0, 0)
 
  Repeat
    oldPos = Loc(0)           ; <-- @Sicro's workaround to check if file read operation was successfull
    value = ReadByte(0)
    newPos = Loc(0)
    If oldPos <> newPos
      Debug value
    Else
      Debug "ReadByte: Error"
      Break
    EndIf
  ForEver
 
  CloseFile(0)
Else
  Debug "File could not be created!"
EndIf

Re: File Library: add GetLastFileError(file) command

Posted: Sun Feb 16, 2020 3:21 am
by BarryG
Deleted as my statement has been explained by kurzer elsewhere.

Re: File Library: add GetLastFileError(file) command

Posted: Sun Feb 16, 2020 10:26 am
by Josh
I think it would be enough if 'Loc' would always return the calculated value after a read access, so the value is not limited with 'Lof'. Then a check would be possible with a simple 'If Loc(0) > Lof(0)'.
BarryG wrote:No file has a byte value of -1
How do you get this nonsense? A Byte is a Byte and -1 is a valid value.

Re: File Library: add GetLastFileError(file) command

Posted: Sun Feb 16, 2020 9:26 pm
by Kurzer
BarryG wrote:No file has a byte value of -1 (as seen in any hex editor: all byte values are $00 to $FF), so ReadByte() should just return -1 for a read error instead of 0.
BarryG, see here for an explanation.

Kurzer

Re: File Library: add GetLastFileError(file) command

Posted: Sun Feb 16, 2020 9:47 pm
by BarryG
Thank you, Kurzer.