File not found but it was selected by file requester

Just starting out? Need help? Post your questions and find answers here.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 593
Joined: Tue Jan 04, 2011 6:21 pm

File not found but it was selected by file requester

Post by SPH »

hi,
I have 3 gadgetText (163, 166 and 169)
i use a file requester to fill them. The paths are therefore all good (not files coming from the root C:/. However, I can't make " If ReadFile(6, a$) " understand that the file is necessarily good and contains several Bytes.
It debugs the line:
Debug " error : "+a$"

I don't understand anything...

Code: Select all

          If CreateFile(7, "f:\tmp\file.ini")
            For i= 163 To 169 Step 3
              a$= GetGadgetText(i)
              If ReadFile(6, a$) 
                Debug Lof(6)
                WriteStringN(7, a$)
                Debug a$
                CloseFile(6)
              Else
                WriteStringN(7, "")
                Debug " error : "+a$
              EndIf
             Next
            CloseFile(7)
          EndIf

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
User avatar
Skipper
User
User
Posts: 59
Joined: Thu Dec 19, 2024 1:26 pm
Location: Europe

Re: File not found but it was selected by file requester

Post by Skipper »

CloseFile(6) ? That's not the one you opened.
<< Win-11 (x64: XEON + i5) / Mint linux (x64: i3) / MacOS Monterey (Intel x64) >>
miso
Enthusiast
Enthusiast
Posts: 491
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: File not found but it was selected by file requester

Post by miso »

Why don't you use filesize() to check if file is empty?

Edit: made some modification, that should hopefully point out the problem. Please try it in your main code.

Code: Select all

          If CreateFile(7, "file.ini")
            For i= 163 To 169 Step 3
              a$= GetGadgetText(i)
              If FileSize(a$) = -2 : Debug "error, "+ a$ + " is a directory":EndIf
              If FileSize(a$) = -1 : Debug "error, file "+ a$ + " is not found.":EndIf
              If FileSize(a$) = 0 : Debug "error, file "+ a$ + " is found, but empty.":EndIf
              If FileSize(a$) > 0 : Debug "Pass, file "+ a$ + " is found, and contains"+ Str(FileSize(a$))+" bytes data.":EndIf

              If ReadFile(6, a$) 
                Debug Lof(6)
                WriteStringN(7, a$)
                Debug a$
                CloseFile(6)
              Else
                WriteStringN(7, "")
                Debug " error : "+a$
              EndIf
             Next
            CloseFile(7)
          EndIf
Last edited by miso on Tue Jan 21, 2025 2:36 pm, edited 1 time in total.
threedslider
Enthusiast
Enthusiast
Posts: 432
Joined: Sat Feb 12, 2022 7:15 pm

Re: File not found but it was selected by file requester

Post by threedslider »

You want something like this

Code: Select all

If CreateFile(7, "file.ini")
  
   
  WriteStringN(7, "File preferences")
  WriteStringN(7, "audio preferences")
      

  CloseFile(7)
EndIf

If ReadFile(7,"file.ini")
  
  Debug  ReadString(7)
  Debug  ReadString(7)
  
  CloseFile(7)
EndIf
User avatar
SPH
Enthusiast
Enthusiast
Posts: 593
Joined: Tue Jan 04, 2011 6:21 pm

Re: File not found but it was selected by file requester

Post by SPH »

miso wrote: Tue Jan 21, 2025 2:05 pm Why don't you use filesize() to check if file is empty?
Because the file is not found !!

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
User avatar
SPH
Enthusiast
Enthusiast
Posts: 593
Joined: Tue Jan 04, 2011 6:21 pm

Re: File not found but it was selected by file requester

Post by SPH »

I resigned myself to not asking this:
"If ReadFile(6, a$) "
because in the "Else" part, the file is not found!

I'm studying to find a complete and short example for Fred

Code: Select all

          If CreateFile(7, GetPathPart(ProgramFilename())+"MK3.ini")
            Debug "---"
          
            For i= 163 To 169 Step 3
              a$= GetGadgetText(i)
              ;If ReadFile(6, a$) ;-If ReadFile(6, a$) 
                WriteStringN(7, a$)
                Debug a$
                ;CloseFile(6)
              ;Else
              ; Debug "no file!"
              ;EndIf
              
            Next
            
            CloseFile(7)
          EndIf

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
Axolotl
Addict
Addict
Posts: 872
Joined: Wed Dec 31, 2008 3:36 pm

Re: File not found but it was selected by file requester

Post by Axolotl »

Hi SPH,

maybe File #6 cannot be opend because of access violation?
If ReadFile() returns 0 it could not open the file, does not say it is not existing.
You can try some flags like #PB_File_SharedRead and/or #PB_File_SharedWrite.
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).
User avatar
SPH
Enthusiast
Enthusiast
Posts: 593
Joined: Tue Jan 04, 2011 6:21 pm

Re: File not found but it was selected by file requester

Post by SPH »

I found it!

Run this code and enter 3 different files grave to the filerequester.
It works!

Now, comment out line 6 and do the test again: it fails

So you have to be careful to close an open channel!!!!

Code: Select all

a$ = OpenFileRequester("Choose file 1","c:\","*.*",0)
b$ = OpenFileRequester("Choose file 2","c:\","*.*",0)
c$ = OpenFileRequester("Choose file 3","c:\","*.*",0)

If ReadFile(3, b$)
CloseFile(3)  
EndIf
  
If CreateFile(7, GetPathPart(ProgramFilename())+"MKx.ini")
  Debug "---"
  
    If ReadFile(6, a$) ;-If ReadFile(6, a$) 
    WriteStringN(7, a$)
    Debug a$
    CloseFile(6)
    Else
     Debug "no file!"
    EndIf
    
    If ReadFile(6, b$) ;-If ReadFile(6, a$) 
    WriteStringN(7, b$)
    Debug b$
    CloseFile(6)
    Else
     Debug "no file!"
    EndIf
    
    If ReadFile(6, c$) ;-If ReadFile(6, a$) 
    WriteStringN(7, c$)
    Debug c$
    CloseFile(6)
    Else
     Debug "no file!"
    EndIf
    
  
  CloseFile(7)
EndIf


;;;;;;;;
Debug "---"
Debug "Resultat : "

If ReadFile(0, GetPathPart(ProgramFilename())+"MKx.ini")   ; Si le fichier peut être lu , on continue...
    While Eof(0) = 0           ; Boucle tant que la fin du fichier n'est pas atteinte. (Eof = 'End Of File') 
      Debug ReadString(0)      ; Affiche ligne par ligne le contenu du fichier
    Wend
    CloseFile(0)               ; Ferme le fichier précédemment ouvert
  Else
    MessageRequester("Information","Impossible d'ouvrir le fichier!")
  EndIf

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: File not found but it was selected by file requester

Post by Fred »

Yes, if a file is opened, you can open it a second time unless you put the ReadShared flag: https://www.purebasic.com/documentation ... dfile.html
Post Reply