testFile$ = "myTestFile"
testFileID1 = 101 ; -- as a variable
#testFileID2 = 101 ; -- as a constant
;--------------------------------------------------------
If ReadFile(0,testFile$)
Format = ReadStringFormat(0) ; -- works
;--------------------------------------------------------
If ReadFile(testFileID1, testFile$)
Format = ReadStringFormat(testFileID1) ; -- error: the specific file is not initialised
;---------------------------------------------------------
If ReadFile(#testFileID2, testFile$)
Format = ReadStringFormat(#testFileID2) ; -- error: the specific file is not initialised
I'm not understanding the issue here.
Any help is appreciated.
Thanks in advance.
I understand about #PB_File_SharedRead, but if that's not used then shouldn't "If ReadFile" return 0 then? And thus prevent OP's situation with ReadStringFormat?
BarryG wrote: Wed May 01, 2024 7:39 am
I understand about #PB_File_SharedRead, but if that's not used then shouldn't "If ReadFile" return 0 then? And thus prevent OP's situation with ReadStringFormat?
Yes, but CaptBenB's code was anyway not executable. So my answer was more general.
STARGÅTE wrote: Wed May 01, 2024 7:18 am
If you want to start reading the same file multiple times, you need #PB_File_SharedRead, otherwise the file is locked by the first ReadFile()
Or you need to close the file before attempting to re-open. This works:
testFile$ = "myTestFile"
testFileID1 = 101 ; -- as a variable
#testFileID2 = 101 ; -- as a constant
;--------------------------------------------------------
If ReadFile(0,testFile$)
Format = ReadStringFormat(0)
CloseFile(0)
EndIf
;--------------------------------------------------------
If ReadFile(testFileID1, testFile$)
Format = ReadStringFormat(testFileID1)
CloseFile(testFileID1)
EndIf
;---------------------------------------------------------
If ReadFile(#testFileID2, testFile$)
Format = ReadStringFormat(#testFileID2)
CloseFile(#testFileID2)
EndIf
Sorry I should have been more clear in my original post.
That was three different attempts in three different routines.
I must have had a typo somewhere - it could have been a hidden character.
After I tried several more times, I was able to get the code to work using any of the three methods.
My original thought was that any of those methods should work - and ultimately they did and is why I'm suspecting that there was a hidden character somewhere in my original code preventing the proper execution.
Even if it is often omitted in the examples here (for the sake of simplicity).
Just because it worked doesn't mean it works. PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).