User Tools

Site Tools


k7:k7.4:k7.4.8:k7.4.8.1:start

7.4.8.1 Export and import of native arrays

To save a native array in a file or to import the contents of such a file back into an array (data import), you can use a Gambas-specific and binary file format. Gambas 3 can realize a simple Array-Export and Array-Import if the array is serialized via Write written to or read from a file stream:

If Dialog.SaveFile() Then Return
hFile = Open Dialog.Path For Write Create
  Write #hFile, aNames As ARRAY
Close #hFile
If Dialog.OpenFile() Then Return
hFile = Open Dialog.Path For Read
  aNames = Read #hFile As ARRAY
Close #hFile

The project pursues the following strategy:

  • First, a native array is created and filled with fixed data.
  • The array elements are then displayed in the IDE console for control.
  • Then you can export the created array (without receipt in case of success)? file save dialog (binary, gambas-specific file) or
  • import an array after a file open dialog from the export file.
  • Finally, the array elements of the imported array are displayed in the IDE console.

The strategy is implemented in the source code. The complete project can be found in the download area.

' Gambas class file
 
Public aNames As String[]
 
Public Sub Form_Open()
  FMain.Center
  FMain.Resizable = False
  If Not Exist(Application.Path &/ "array.dat") Then
     SetEnabled(True, False, False)
  Else
     SetEnabled(True, False, True)
  Endif
End ' Form_Open()
 
Public Sub btnSetArray_Click()
  aNames = ["Bear", "Tit", "Eagle", "Eagle Owl", "Badger", "Fox"]
  ShowArrayElements(aNames)
  SetEnabled(True, True, False)
End ' btnClassArray_Click()
 
Public Sub btnArrayDatenExport_Click()
  Dim hFile As File
 
' Saving all elements of the array 'aNames' in a binary file
  Dialog.Title = "Array export to a binary file".
  Dialog.Filter = ["*.dat", "Data Files"]
  Dialog.Path = Application.Path &/ "array.dat"
 
  If Dialog.SaveFile() Then Return
  hFile = Open Dialog.Path For Write Create
  Write #hFile, aNames As ARRAY
 
  Close #hFile
 
  SetEnabled(True, True, True)
 
End ' btnDatenExport_Click()
 
Public Sub btnArrayDatenImport_Click()
  Dim hFile As File
 
' Read in all elements of the array 'aNames' from a binary file.
  Dialog.Title = "Array import from a binary file".
  Dialog.Filter = ["*.dat", "Data files"]
  Dialog.Path = Application.Path &/ "array.dat"
 
  If Dialog.OpenFile() Then Return
  hFile = Open Dialog.Path For Read
  aNames = Read #hFile As ARRAY
 
  Close #hFile
 
  ShowArrayElements(aNames)
  SetEnabled(True, True, True)
 
End ' btnDatenImport_Click()
 
Private Sub ShowArrayElements(aArray As String[])
  Dim sElement As String
 
' Display of all data sets (console IDE)
  For Each sElement In aArray
      Print sElement,
  Next ' sElement
  Print
 
End ' ShowArrayElements(aArray As String[])
 
Public Sub SetEnabled(bAR As Boolean, bEX As Boolean, bIM As Boolean)
 
  btnSetArray.Enabled = bAR
  btnArrayDatenExport.Enabled = bEX
  btnArrayDatenImport.Enabled = bIM
 
End ' SetEnabled(..)

Issues in the console of the Gambas IDE:

Bear   Tit   Eagle   Owl   Badger   Fox ' Elements Original Array
Bear   Tit   Eagle   Owl   Badger   Fox ' Elements Import Array

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k7/k7.4/k7.4.8/k7.4.8.1/start.txt · Last modified: 05.02.2022 (external edit)

Page Tools