ReadByte (and others) return code
ReadByte (and others) return code
As I already mentioned some time ago:
according to the manual ReadByte returns the read byte if successful and zero if not successfull. But we all know that zero is a valid value for a byte. So are there any plans to correct this?
How can a program distinguish between byte value of 0 (success) and error (return code also 0)?
Maybe a return variable should be more than one byte long so a return value of 256 (or more) means error. And then logical operater bitwise AND 0xFF is done to keep only the required bits.
The same goes for other Read commands.
Thanks.
Saso
according to the manual ReadByte returns the read byte if successful and zero if not successfull. But we all know that zero is a valid value for a byte. So are there any plans to correct this?
How can a program distinguish between byte value of 0 (success) and error (return code also 0)?
Maybe a return variable should be more than one byte long so a return value of 256 (or more) means error. And then logical operater bitwise AND 0xFF is done to keep only the required bits.
The same goes for other Read commands.
Thanks.
Saso
Re: ReadByte (and others) return code
-1
If you need that information use ReadData().
It will just add more overhead to the other functions which is not needed.
Optional just check the file pointer...
If you need that information use ReadData().
It will just add more overhead to the other functions which is not needed.
Optional just check the file pointer...
- NicTheQuick
- Addict
- Posts: 1224
- Joined: Sun Jun 22, 2003 7:43 pm
- Location: Germany, Saarbrücken
- Contact:
Re: ReadByte (and others) return code
You could create your own version of ReadByte and similar by using Modules and and Wrapper like so:
Code: Select all
DeclareModule FileWrapper
Macro ReadByte(hFile)
FileWrapperImpl::_ReadByte(hFile)
EndMacro
EndDeclareModule
Module FileWrapper
EndModule
DeclareModule FileWrapperImpl
Declare _ReadByte(hFile.i)
EndDeclareModule
Module FileWrapperImpl
Procedure.i _ReadByte(hFile.i)
Protected b.b
If Not IsFile(hFile)
ProcedureReturn 257 ; file not initialized
ElseIf Not ReadData(hFile, @b, SizeOf(Byte)) = SizeOf(Byte)
ProcedureReturn 256; byte could not be read
EndIf
ProcedureReturn b
EndProcedure
EndModule
; Calls the original ReadByte
;ReadByte(1)
; From now on only the overwritten version of ReadByte is used
UseModule FileWrapper
; Calls the new ReadByte
Debug ReadByte(1)
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Re: ReadByte (and others) return code
Thank you all for the hints etc.. but I think that this problem should be addressed in PB itself.
Saso
Saso
Re: ReadByte (and others) return code
Another option:
Code: Select all
EnableExplicit
Macro ReadByte(_hfile_,_buffer_);reads one byte - returns #True/#False
Bool(ReadData(_hfile_,_buffer_,1) = 1)
EndMacro
Procedure.i Main()
Protected hfile.i
Protected first_byte.b
hfile = ReadFile(#PB_Any,ProgramFilename())
If hfile
If ReadByte(hfile,@first_byte)
Debug "0x" + Hex(first_byte,#PB_Byte)
EndIf
CloseFile(hfile)
EndIf
ProcedureReturn #Null
EndProcedure
Main()
End
Re: ReadByte (and others) return code
When the documentation writes "Returns the read byte or zero if there was an error.", it just means: in case of any error, the return value is defined as 0, and is not undetermined. The return value cannot be used for error explanation. Of cause not, because the return value range is the full type range. Mixing error numbers and functional values it not a good idea and finally impossible for ReadQuad or ReadString.
For an advanced error investigation, PB would need something like LastError or in case of the file library: LastFileError(File). But at the moment PB has no special error codes in the file library.
For an advanced error investigation, PB would need something like LastError or in case of the file library: LastFileError(File). But at the moment PB has no special error codes in the file library.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Re: ReadByte (and others) return code
I think this was discussed before, but why can't it just return -1 for an error instead of 0? When I look at all bytes of a file with a hex editor, they all show a value of 0-255 ($0-$FF), so if hex editors show and support that range, then why can't PureBasic use -1 for an error?
Re: ReadByte (and others) return code
To refresh your memory:BarryG wrote: ↑Sat Aug 14, 2021 1:12 am I think this was discussed before, but why can't it just return -1 for an error instead of 0? When I look at all bytes of a file with a hex editor, they all show a value of 0-255 ($0-$FF), so if hex editors show and support that range, then why can't PureBasic use -1 for an error?
Link to similar discussion in the General Discussion Forum: ReadByte/ReadAsciiCharacter return value on error.
Link to a resulting Feature Request: File Library: add GetLastFileError(file) command.
In the last thread link you asked the same question, it was answered by kerzur, and you then deleted your question because he answered it.
Re: ReadByte (and others) return code
Yeah, my memory isn't as good as it used to be. Thanks for the link.
Re: ReadByte (and others) return code
Instead of reinventing the wheel - what is wrong with ReadData()?
Re: ReadByte (and others) return code
I'm losing my mind.
Last edited by BarryG on Sat Aug 14, 2021 11:34 am, edited 1 time in total.
Re: ReadByte (and others) return code
No it doesnt:
Therefore the return value can be used.Returns the number of bytes actually read from the file. If there is an error, the return value is zero
Here is the POC:
Code: Select all
EnableExplicit
;override ReadByte() with our Macro
Macro ReadByte(_hfile_,_buffer_);reads one byte - returns #True on succes and #False if an error occured
Bool(ReadData(_hfile_,_buffer_,1) = 1)
EndMacro
Procedure.i Main()
Protected hfile.i
Protected first_byte.b
hfile = ReadFile(#PB_Any,ProgramFilename())
If hfile
;poc: set the filepointer to the end to generate an error!
FileSeek(hfile,Lof(hfile))
;lets try reading a byte :>
If ReadByte(hfile,@first_byte)
Debug "0x" + Hex(first_byte,#PB_Byte)
Else
Debug "byte could not be read!"
EndIf
CloseFile(hfile)
EndIf
ProcedureReturn #Null
EndProcedure
Main()
End