write an array to file records / read file records into an array

Post

Posted
Rating:
#1 (In Topic #1383)
Avatar
Regular
mandarin is in the usergroup ‘Regular’
 Hello!

How can I write an array into a file (and what type of appropriate file? csv perhaps?),…
…and then read back the file records into that array?

Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’

mandarin said

Hello!

How can I write an array into a file (and what type of appropriate file? csv perhaps?),…
…and then read back the file records into that array?

An array of what?

Maybe use Setting.class

Dim aStrings as String[] = ["hi", "halo", "ola"]

File.Save(sMyFileName, Settings.ToString(aStrings))

or use your programs settings

Settings["MySavedArray"] = aStrings
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
mandarin is in the usergroup ‘Regular’
Actually, I wrote successfully a single-dimensional array into a (text) file, like this:

Code (gambas)

  1. Dim sFilePath As String = User.Home &/ "Some-path-to-my-file.txt"
  2. Dim hFile As File
  3. Dim MyOwnArray As New Boolean[36]
  4.  
  5. hFile = Open sFilePath For Write
  6. For ii = 0 To 35
  7.   MyOwnArray[ii] = False
  8.   Print #hFile, MyOwnArray[ii]
  9. Close #hFile

Indeed, this code wrote a 36-lines text file with the word "False" on each line; where later I changed (using Linux Mint Text Editor) some lines to "True", just to check if I can read them back to MyOwnArray.
Then, I tried this code:

Code (gambas)

  1. Dim MyOwnArray As String[] = []
  2.  
  3. stream = Open sFilePath For Read
  4.   Read #stream, line
  5.   MyOwnArray.Add(line)
  6.  Print "Error reading from file: "; Error.Text
  7. Return MyOwnArray

but failed.

So, these questions arise:
- Are text files suitable for every case?
- If not, what type of files is suitable for what job? (Saving an array, for instance.)
- What is the right code to read the records back to the reciprocal array records?

Ps 1: I used two different Public Sub routines (not mentioned here) to write and read.
Ps 2: I created (and gave access to) my file with the following code, which works fine:

Code (gambas)

  1. If Not Exist(sFilePath) Then
  2.   hFile = Open sFilePath For Create
  3.   Shell "stat --format '%a' " & Quote(sFilePath) To var1Shellvar1
  4.   If Val(var1Shellvar1) <> 600 Then
  5.     Shell "chmod 0600 " & Quote(sFilePath) Wait

Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
Try Str() and Val() to save/load values other than String

Print #hFile, Str(MyOwnArray[ii])

then read back with….

  Read #stream, line
  MyOwnArray.Add(Val(line))

Then Boolean values should work as expected

This code is wrong..

Code (gambas)

  1. Dim MyOwnArray As String[] = []
  2.  
  3. stream = Open sFilePath For Read
  4.   Read #stream, line
  5.   MyOwnArray.Add(line)
  6.  Print "Error reading from file: "; Error.Text
  7. Return MyOwnArray
  8.  

Your use of Try is not correct
The first Try is not needed. (and Try should be followed by a function on the same line) /lang/try - Gambas Documentation
"End Try" is not a command

Code (gambas)

  1. Dim MyOwnArray As String[] = []
  2.  
  3. stream = Open sFilePath For Read
  4.   Read #stream, line
  5.   MyOwnArray.Add(line)
  6.  
  7. Return MyOwnArray
  8.  
  9. Catch  ' code past Catch is only run if an error occurs.
  10.  Print "Error reading from file: "; Error.Text
  11.  

Or all that code can be this…

Code (gambas)

  1. Dim MyOwnArray As String[] = Split(File.Load(sFilePath, "\n", Null, True))
  2.  
  3.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’
So, these questions arise:
- Are text files suitable for every case?
Well only for data that can be converted to a string

- If not, what type of files is suitable for what job? (Saving an array, for instance.)
- What is the right code to read the records back to the reciprocal array records?

Like i said in my first post Settings.class is good for handling many different formats.
It can be used on local files of choice.

Save

Code (gambas)

  1.  
  2. Dim hSet As Settings = New Settings(sFilePath)
  3. Dim MyArray As Variant[] = [True, False, 256, "text data"]
  4.  
  5. hSet["MyArray"] = MyArray
  6. hSet.Save
  7.  
  8.  

Load

Code (gambas)

  1.  
  2. Dim hSet As Settings = New Settings(sFilePath)
  3. Dim MyArray As Variant[]
  4.  
  5. MyArray = hSet["MyArray", Null]
  6.  
  7.  
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
vuott is in the usergroup ‘Regular’

mandarin said

How can I write an array into a file ……
…and then read back the file records into that array?
Hello,
did you try ".Write()" and ".Read()" Methods of array ?

e.g.:
/comp/gb/byte - Gambas Documentation[]/read
/comp/gb/byte - Gambas Documentation[]/write

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
mandarin is in the usergroup ‘Regular’
@BruceSteers:

Thanks! Finally, this code worked very well:

Code (gambas)

  1. Dim sFilePath As String = User.Home &/ "some-path-to/trialfile"
  2. Dim hFile As File
  3.  
  4. hFile = Open sFilePath For Read
  5.   Dim MyOwnArray As String[] = Split(File.Load(sFilePath), gb.NewLine, "", True)
  6. Close #hFile
  7.  
  8. ' assuring the right result - see attached screen captures
  9. For ii = 0 To 35
  10.   Print MyOwnArray[ii]

(When I entered "for ii = 1 to 36", I got a program halt; but, due to my experience, I changed the ii range from 0 to 35, and everything worked as expected.)


Image

(Click to enlarge)

Image

(Click to enlarge)



@vuott:

Thanks, I am still learning! Simple code first!

Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’
you are welcome.

btw File.Load does not require you Open and Close the file, it does it for you

Code (gambas)

  1. Dim sFilePath As String = User.Home &/ "some-path-to/trialfile"
  2. Dim hFile As File
  3.  
  4.   Dim MyOwnArray As String[] = Split(File.Load(sFilePath), gb.NewLine, "", True)
  5.  
  6. ' assuring the right result - see attached screen captures
  7. For ii = 0 To 35
  8.   Print MyOwnArray[ii] , CBool(MyOwnArray[ii])  ' show the string and it's real Boolean value
  9.  
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Guru
cogier is in the usergroup ‘Guru’
I have used this type of command quite often, this is how I would do it: -

Code (gambas)

  1. Dim MyOwnArray As String[] = Split(File.Load(User.Home &/ "some-path-to/trialfile"), gb.NewLine, "", True)
  2.  
  3.   For ii As Integer = 0 To MyOwnArray.Max
  4.     Print MyOwnArray[ii], CBool(MyOwnArray[ii])
  5.   Next
Online now: No Back to the top

Post

Posted
Rating:
#10
Avatar
Regular
mandarin is in the usergroup ‘Regular’
  cogier :

Thanks! I am keeping notes of everything.

Online now: No Back to the top
1 guest and 0 members have just viewed this.