File Library: add GetLastFileError(file) command

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Tristano
Enthusiast
Enthusiast
Posts: 190
Joined: Thu Nov 26, 2015 6:52 pm
Location: Italy
Contact:

File Library: add GetLastFileError(file) command

Post 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
The PureBASIC Archives: FOSS Resources:
BarryG
Addict
Addict
Posts: 3324
Joined: Thu Apr 18, 2019 8:17 am

Re: File Library: add GetLastFileError(file) command

Post by BarryG »

Deleted as my statement has been explained by kurzer elsewhere.
Last edited by BarryG on Sun Feb 16, 2020 9:47 pm, edited 1 time in total.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: File Library: add GetLastFileError(file) command

Post 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.
sorry for my bad english
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 666
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: File Library: add GetLastFileError(file) command

Post 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
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2024: 56y
"Happiness is a pet." | "Never run a changing system!"
BarryG
Addict
Addict
Posts: 3324
Joined: Thu Apr 18, 2019 8:17 am

Re: File Library: add GetLastFileError(file) command

Post by BarryG »

Thank you, Kurzer.
Post Reply