Writing to file-Reading from file?

BlitzMax Forums/BlitzMax Beginners Area/Writing to file-Reading from file?

Amon_old(Posted 2005) [#1]
First, can you incbin text files?

I have a factory in my game where people are able to build their own puzzle. Each tile is stored as a number from 1-30 in the array. If i wanted to write this information to a text file what commands would I use? Also the same for reading it back in.

Do I use writeint(), Readint() or one of the others like writebyte,readbyte?

a code sample would be nice. I've looked at the docs but the commands are not documented properly (no example given ect.).


skidracer(Posted 2005) [#2]
The commands WriteInt etc. are for writing binary files, if you want to use text files (files you can read and write with text editors etc.) stick with the WriteLine / ReadLine commands.

Here is an example of writing and reading some integers to and from a text file:
Local x=20
Local y=40

out=WriteStream("test.txt")
If Not out RuntimeError "Failed to open a WriteStream to file test.txt"
WriteLine out,x
WriteLine out,y
CloseStream out

in=ReadStream("test.txt")
If Not In RuntimeError "Failed to open a ReadStream to file test.txt"
x1=Int(ReadLine(in))
y1=Int(ReadLine(in))
CloseStream in

Print "x1="+x1
Print "y1="+y1



Amon_old(Posted 2005) [#3]
Thanks very much skidracer. :)


klepto2(Posted 2005) [#4]
It depends what type your Array is but the main framework should something like that:

if filetype("yourfile.dat") = 0 then 
 new_file = createfile("yourfile.dat")
end if

'To open the file for writing :
write_file = Writefile("yourfile.dat")
For x = 1 to 30
Writeint(write_file,YourArray[x])
next
Closestream(write_file)

'To open the file for Reading :
Read_file = Readfile("yourfile.dat")

while not EOF(Read_file)
YourArray[X] = Readint(Read_file)
x = x + 1
wend

Closestream(Read_file)

' You can also use Openfile with your File to open it for read and write the same time


Yes you can use incbin with Textfiles but you can only read them. Writing of Incbin files is not allowed.
Don't forget to change the path of your file to 'incbin::yourfile.dat' when using incbin.