[Gambas-user] Saving integer array into file

Rolf-Werner Eilert eilert-sprachen at ...221...
Tue May 18 15:43:13 CEST 2010


Am 18.05.2010 15:06, schrieb Fabien Bodard:
> Le 18 mai 2010 13:30, Benoît Minisini<gambas at ...1...>  a écrit :
>>> Am 18.05.2010 13:09, schrieb Fabien Bodard:
>>>> 2010/5/18 Rolf-Werner Eilert<eilert-sprachen at ...221...>:
>>>>> Just had this idea:
>>>>>
>>>>> DIM myArray AS NEW Integer[]
>>>>>
>>>>> 'code filling myArray with integer values
>>>>>
>>>>> File.Save(myArray)
>>>>>
>>>>> Well, this won't be possible: File.Save only saves strings. Or is there
>>>>> another way by somehow copying the integer array into a string and
>>>>> saving that instead?
>>>>>
>>>>> Anyway, this would make you a direct copy of the array's binary
>>>>> structure in a file (instead of having to Cstr() and save each value as
>>>>> separated strings).
>>>>>
>>>>> Is there a way to do this in Gambas2?
>>>>>
>>>>> Regards
>>>>>
>>>>> Rolf
>>>>>
>>>>> ------------------------------------------------------------------------
>>>>> ------
>>>>>
>>>>> _______________________________________________
>>>>> Gambas-user mailing list
>>>>> Gambas-user at lists.sourceforge.net
>>>>> https://lists.sourceforge.net/lists/listinfo/gambas-user
>>>>
>>>> well there is the gb.stetting that is able to save data structure or
>>>> simply that :
>>>>
>>>>
>>>> dim aMyArray as new Integer[]
>>>>
>>>> 'do what you want to fill it
>>>>
>>>> file.save("myfile", aMyArray.Join())
>>>>
>>>>
>>>>
>>>> to load the array line :
>>>>
>>>> aMyArray=split(file.load("myfile"))
>>>
>>> Ok, but for this function the work is the same: stuff everything into a
>>> string by converting each single element into a string and adding
>>> separators / reading the string back, splitting the elements by their
>>> separators and converting each element into an integer.
>>>
>>> It's C but it's not as fast as it could be. My idea was mainly about speed.
>>>
>>> Rolf
>>>
>>
>> Look at the Read() and Write() methods of the Array classes.
>>
>> Regards,
>>
>> --
>> Benoît Minisini
>
> Benoit, is there something about that in the help ?.... i don't see
> anything about a read or a wirte method :/

I found it: Make

DIM myArray as Integer[]

and try

myArray.Read

moving the cursor on Read, press F2 and up comes:

Integer[].Read (gb)
Syntax
SUB Read ( Stream AS Stream [ , Start AS Integer, Length AS Integer ] )
  Fills an array by reading the data directly from a stream.
Start specifies where the data are stored in the array. By default, data 
are stored from the beginning.
Length is the number of elements that will be read from the file. By 
default, data are read until the end of the array is reached.


There's no example given, so let us invent one:



DIM myArray AS Integer[]
DIM myFile AS Stream
DIM i AS Integer

   for i = 0 to 100
     myArray.Add(i)
   next

   myFile = OPEN "foo.file" FOR WRITE

     myArray.Write(myFile)

   CLOSE myFile

   myArray.Clear

   myFile = Open "foo.file" FOR READ

     myArray.Read(myFile)

   CLOSE myFile


Would it run this way? Was just a guess... I never used READ and WRITE.

It would mean, I could add several such arrays to one stream/file and 
read them back if I know their numbers of elements, right?

Remains one question: do I have to know the number of elements and 
dimensionate the array before, or does this go by itself? (The question 
would be: are the dimensions saved to the stream or only the naked 
data?) From what the help text says, I would guess the latter...



>
> and what is array.bounds exactly ? (i know i'm late for that)
>
>

Never heard of it. No idea, the help doesn't help me here, either, but 
you asked Benoit anyway :-)

Rolf





More information about the User mailing list