It is currently Tue Oct 20, 2020 1:22 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: File Library: add GetLastFileError(file) command
PostPosted: Sat Feb 15, 2020 4:21 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Nov 26, 2015 6:52 pm
Posts: 188
Location: Italy
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:
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:


Top
 Profile  
Reply with quote  
 Post subject: Re: File Library: add GetLastFileError(file) command
PostPosted: Sun Feb 16, 2020 3:21 am 
Offline
Addict
Addict

Joined: Thu Apr 18, 2019 8:17 am
Posts: 1034
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.

Top
 Profile  
Reply with quote  
 Post subject: Re: File Library: add GetLastFileError(file) command
PostPosted: Sun Feb 16, 2020 10:26 am 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 1140
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


Top
 Profile  
Reply with quote  
 Post subject: Re: File Library: add GetLastFileError(file) command
PostPosted: Sun Feb 16, 2020 9:26 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jun 11, 2006 12:07 am
Posts: 573
Location: Near Hamburg
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 5.72 x64, OS: Windows 7 Pro x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age: 52y
"Happiness is a pet." | "Never run a changing system!"


Top
 Profile  
Reply with quote  
 Post subject: Re: File Library: add GetLastFileError(file) command
PostPosted: Sun Feb 16, 2020 9:47 pm 
Offline
Addict
Addict

Joined: Thu Apr 18, 2019 8:17 am
Posts: 1034
Thank you, Kurzer.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye