assigning id to file operations

Just starting out? Need help? Post your questions and find answers here.
CaptBenB
New User
New User
Posts: 7
Joined: Tue Apr 30, 2024 10:34 pm

assigning id to file operations

Post by CaptBenB »

Code: Select all

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.
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: assigning id to file operations

Post by mk-soft »

Hello,

no problem here,
but your code is not complete to make a statement.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
STARGÅTE
Addict
Addict
Posts: 2260
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: assigning id to file operations

Post by STARGÅTE »

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()
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 moreTypeface - Sprite-based font include/module
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: assigning id to file operations

Post by BarryG »

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?
User avatar
STARGÅTE
Addict
Addict
Posts: 2260
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: assigning id to file operations

Post by STARGÅTE »

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.
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 moreTypeface - Sprite-based font include/module
User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: assigning id to file operations

Post by spikey »

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:

Code: Select all

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
  
CaptBenB
New User
New User
Posts: 7
Joined: Tue Apr 30, 2024 10:34 pm

Re: assigning id to file operations

Post by CaptBenB »

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.

Thanks for responding.
Axolotl
Addict
Addict
Posts: 873
Joined: Wed Dec 31, 2008 3:36 pm

Re: assigning id to file operations

Post by Axolotl »

Welcome to the forum.
CaptBenB wrote: Sat May 04, 2024 6:03 am ....
I must have had a typo somewhere - it could have been a hidden character.
....
It is therefore very helpful to use

Code: Select all

EnableExplicit
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).
Post Reply