Page 1 of 1

How to fill an Array in procedure ReadCallback?

Posted: Tue Feb 08, 2022 2:37 pm
by ChrisH
Good afternoon,

I'm new to spider basic, so please excuse this simple question.

Perhaps somebody can help me with this problem: I want to open a file and the read data shall be put in an Array. This does not work. The Array is empty. How can I fill an Array with data read from a file?

Greetings,
Chris

Code: Select all

Global Dim Daten.s (10)

Procedure ReadCallback(Status, Filename$, File, Size)
  If Status = #PB_Status_Loaded
    Debug "File: " + Filename$ + " - Size: " + Size + " bytes"
    
    ; Read the first 10 lines
    ;
    While Eof(0) = 0 And NbLine < 10 
      s$ = ReadString(0)            
      NbLine+1
      Daten(NbLine) = s$
    Wend
    
    CloseFile(0)
    
  ElseIf Status = #PB_Status_Error
    Debug "Error when loading the file: " + Filename$
  EndIf
EndProcedure

Procedure OpenFileRequesterCallback()
  If NextSelectedFile()
    OpenFile(0, SelectedFileID(), @ReadCallback(), #PB_LocalFile)
  EndIf
EndProcedure

Procedure ChooseFileEvent()
  OpenFileRequester("*.txt", @OpenFileRequesterCallback())
EndProcedure

OpenWindow(0, 0, 0, 300, 50, "Read file example", #PB_Window_ScreenCentered)
ButtonGadget(0, 10, 10, 280, 30, "Choose a file...")

BindGadgetEvent(0, @ChooseFileEvent())

For i = 1 To 10
  Debug Daten(i)
Next


Re: How to fill an Array in procedure ReadCallback?

Posted: Tue Feb 08, 2022 3:13 pm
by Peter
The array is not correctly filled until the file has been read:

Code: Select all

Global Dim Daten.s (10)

Procedure DebugDaten()
  
  Protected i
  
  For i = 1 To 10
    Debug Daten(i)
  Next
  
EndProcedure

Procedure ReadCallback(Status, Filename$, File, Size)
  If Status = #PB_Status_Loaded
    Debug "File: " + Filename$ + " - Size: " + Size + " bytes"
    
    ; Read the first 10 lines
    ;
    While Eof(0) = 0 And NbLine < 10 
      s$ = ReadString(0)            
      NbLine+1
      Daten(NbLine) = s$
    Wend
    
    CloseFile(0)
    
    DebugDaten() ; <-- here !
    
  ElseIf Status = #PB_Status_Error
    Debug "Error when loading the file: " + Filename$
  EndIf
EndProcedure

Procedure OpenFileRequesterCallback()
  If NextSelectedFile()
    OpenFile(0, SelectedFileID(), @ReadCallback(), #PB_LocalFile)
  EndIf
EndProcedure

Procedure ChooseFileEvent()
  OpenFileRequester("*.txt", @OpenFileRequesterCallback())
EndProcedure

OpenWindow(0, 0, 0, 300, 50, "Read file example", #PB_Window_ScreenCentered)
ButtonGadget(0, 10, 10, 280, 30, "Choose a file...")

BindGadgetEvent(0, @ChooseFileEvent())


Re: How to fill an Array in procedure ReadCallback?

Posted: Wed Feb 09, 2022 9:36 am
by ChrisH
Okay, thanks!

In fact, I need this Array not within the procedure "ReadCallback". I want to work with this Array, I want to change the values and save the Data in a file again.

So, if I call the procedure DebugDaten() at the end of the program the data are lost. What's my mistake?

Code: Select all

Global Dim Daten.s (10)

Procedure DebugDaten()
  
  Protected i
  
  For i = 1 To 10
    Debug Daten(i)
  Next
  
EndProcedure

Procedure ReadCallback(Status, Filename$, File, Size)
  If Status = #PB_Status_Loaded
    Debug "File: " + Filename$ + " - Size: " + Size + " bytes"
    
    ; Read the first 10 lines
    ;
    While Eof(0) = 0 And NbLine < 10 
      s$ = ReadString(0)            
      NbLine+1
      Daten(NbLine) = s$
    Wend
    
    CloseFile(0)
    
    ;DebugDaten() ; <-- here ! Okay, it works!
    
  ElseIf Status = #PB_Status_Error
    Debug "Error when loading the file: " + Filename$
  EndIf
EndProcedure

Procedure OpenFileRequesterCallback()
  If NextSelectedFile()
    OpenFile(0, SelectedFileID(), @ReadCallback(), #PB_LocalFile)
  EndIf
EndProcedure

Procedure ChooseFileEvent()
  OpenFileRequester("*.txt", @OpenFileRequesterCallback())
EndProcedure

OpenWindow(0, 0, 0, 300, 50, "Read file example", #PB_Window_ScreenCentered)
ButtonGadget(0, 10, 10, 280, 30, "Choose a file...")

BindGadgetEvent(0, @ChooseFileEvent())
DebugDaten () ; This doesn't work

Re: How to fill an Array in procedure ReadCallback?

Posted: Wed Feb 09, 2022 12:06 pm
by Peter
ChrisH wrote: Wed Feb 09, 2022 9:36 amIn fact, I need this Array not within the procedure "ReadCallback".
The array is filled in a specific place in your code. Only here (in ReadCallback() after CloseFile(0)) can you work with the data in the array.

Re: How to fill an Array in procedure ReadCallback?

Posted: Wed Feb 09, 2022 11:16 pm
by William Van Hoecke
ChrisH wrote: Wed Feb 09, 2022 9:36 am So, if I call the procedure DebugDaten() at the end of the program the data are lost. What's my mistake?
SpiderBasic is asynchronous.
You are probably reading the array before it was even filled.
As Peter already pointed out, call whatever code you will use on the array from the end off the ReadCallBack.