Write/Read Array To/From Disk?
BlitzMax Forums/BlitzMax Programming/Write/Read Array To/From Disk?
| ||
I was trying to find a simple way of writing the contents of arrays ( of simple datatypes only, no objects! ) to disk and reading them back in again. I'm using CreateStaticBank() and passing the pointer as VarPtr(MyArray[0]) or VarPtr(MyArray[0,0]) depending on the number of dimensions and then a bank size of 4* the total number of elements in the array. Then just Bank.Save() to write to disk. But I'm just getting a file of zeros, so it looks like I can't create a static bank like this. I was surprised because I actually thought that arrays of simple datatypes like ints and floats were written in blocks of contiguous memory, but now I'm wondering if perhaps that only applies to one dimensions arrays. Is there no easy way to do it? |
| ||
why not use a stream directly?Local a:Int[16,16] Local stream:TStream = WriteStream( "array.data") stream.WriteBytes( a, SizeOf(a)) stream.Close() |
| ||
No particular reason in this instance. It was more the principle of being able to count on arrays being stored in memory like that. The ability to convert them to banks. I was just a bit surprised that it fails. |
| ||
I see what you mean now, i did a test and it seems that C also does this? <edit> So doing [x,y] you get downward and [y,x] you get sequential... |
| ||
This seems to workLocal array:Int[100,100] Local n:Int,n1:Int,c:Int For n=0 Until 100 For n1=0 Until 100 array[n,n1]= c c:+1 Next Next Local bank:TBank = CreateStaticBank(array,SizeOf(array)) For n=0 Until BankSize(bank) Step 4 Print PeekInt(bank,n) Next |