write an array to file records / read file records into an array
Posted
#1
(In Topic #1383)
Regular

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?
Posted
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
Posted
Regular

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:
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:
Posted
Guru

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..
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)
Or all that code can be this…
Posted
Guru

- 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
Load
Posted
Regular

Hello,mandarin said
How can I write an array into a file ……
…and then read back the file records into that array?
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>
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Posted
Regular

Thanks! Finally, this code worked very well:
Code (gambas)
- Close #hFile
- ' assuring the right result - see attached screen captures
(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.)
@vuott:
Thanks, I am still learning! Simple code first!
Posted
Guru

btw File.Load does not require you Open and Close the file, it does it for you
Code (gambas)
- ' assuring the right result - see attached screen captures
Posted
Guru

Posted
1 guest and 0 members have just viewed this.




