Writing an array to a file
Blitz3D Forums/Blitz3D Beginners Area/Writing an array to a file
| ||
G day all. Its taken me a long time but i have finally got my head around arrays. Now i would like to write an array to a file. I have tried a couple of ways but i am not getting it the way i would like. Here is what i am trying to achieve. I would like the .dat file to look this: 1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1, here is my first code. output = WriteFile("level.dat") Dim grid(9,4) For rows = 0 To 9 For cols = 0 To 4 grid(rows,cols) = 1 Write grid(rows,cols) WriteLine output,grid(rows,cols) + "," ; writline here gives me the number 1 repeated down Next ; the .dat file Print"" Next Print "" Print "Press any key to continue" CloseFile(output) WaitKey() End this gives me this in the .dat file. 1, 1, 1, 1, 1, 1, ........ and here is the second way i tried which gives me an array index out of bounds output = WriteFile("level.dat") Dim grid(9,4) For rows = 0 To 9 For cols = 0 To 4 grid(rows,cols) = 1 Write grid(rows,cols) Next WriteLine output,grid(rows,cols) + "," ; & wrteline here causes an array index out of bounds Print"" Next Print "" Print "Press any key to continue" CloseFile(output) WaitKey() End Any help would be much appreciated. Cheers :) |
| ||
use writebyte for each cell instead... then just read back the same way maybe write a little header that says how many rows/columns.. |
| ||
Thanks I have tried replacing writeline with writebyte & now my .dat file looks weird. i have a 9,9 which is from another piece of code i added which i want, but following that i get a line of blue rectangles any chance i could get an example. Cheers :) |
| ||
Cheers _Skully I have done a bit more looking around & trials have managed to get it working the way i want. Here it is if anyone wants a look at how iv'e done it or any sugestions on how it could be done better. output = WriteFile("level.dat") ;Will write to a file called level.dat when called. r = Input$("Input rows: ") c = Input$("Input coloumns: ") WriteLine output,r + "," + c ; Use this to write values fo array dimentions to the .dat file WriteLine output,"" ; Creates a new line in the .dat file temp$="" ; Creates an empty string r = r - 1 c = c - 1 Dim grid(r,c) ; Uses c & r to define array dimentions For rows = 0 To r For cols = 0 To c grid(rows,cols) = 1 Next Next For rows = 0 To r temp = "Data " ; This adds the Data command to the start of the lines in the .dat file For cols = 0 To c If cols < c Then ; I use this to check loops to see if i need a comma Write grid(rows,cols)+","; I only use this to get a visual desplay of what i am writing out temp = temp + Str$(grid(rows,cols) + ",") ; places a comma after a number if it is needed ; & converts the array into a string for writing ; out Else If cols >= c Then ; I use this to check loops to see if i don't need a comma Write grid(rows,cols) ; I only use this to get a visual desplay of what i am writing out temp = temp + Str$(grid(rows,cols)) ; converts the array into a string for writing ; out without the comma End If Next WriteLine (output,temp) ; Writes the current line stored as temp to the .dat file Print"" ; Gives a new line temp = "" ; Clears the temp$ string to get ready for the next string to be created. Next WriteLine output,"" ; Creates a new line in the .dat file CloseFile(output) Print "" Print "Press any key to continue" WaitKey() End |