Page 1 of 1

File not found but it was selected by file requester

Posted: Tue Jan 21, 2025 1:44 pm
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

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

Posted: Tue Jan 21, 2025 1:58 pm
by Skipper
CloseFile(6) ? That's not the one you opened.

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

Posted: Tue Jan 21, 2025 2:05 pm
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

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

Posted: Tue Jan 21, 2025 2:32 pm
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

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

Posted: Tue Jan 21, 2025 3:07 pm
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 !!

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

Posted: Tue Jan 21, 2025 3:31 pm
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

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

Posted: Tue Jan 21, 2025 4:08 pm
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.

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

Posted: Tue Jan 21, 2025 4:44 pm
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

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

Posted: Tue Jan 21, 2025 4:47 pm
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