How to fill an Array in procedure ReadCallback?

Just starting out? Need help? Post your questions and find answers here.
ChrisH
Posts: 2
Joined: Sat Feb 05, 2022 3:14 pm

How to fill an Array in procedure ReadCallback?

Post 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

User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: How to fill an Array in procedure ReadCallback?

Post 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())

ChrisH
Posts: 2
Joined: Sat Feb 05, 2022 3:14 pm

Re: How to fill an Array in procedure ReadCallback?

Post 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
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: How to fill an Array in procedure ReadCallback?

Post 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.
User avatar
William Van Hoecke
Posts: 50
Joined: Tue Oct 22, 2019 12:09 pm

Re: How to fill an Array in procedure ReadCallback?

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