How to write and read a binary file?

BlitzMax Forums/BlitzMax Programming/How to write and read a binary file?

Nennig(Posted 2016) [#1]
Hi,

I am trying to learn how to write and read a binary file.
This binary file would need to store a string e.g. "Hello World" and I would need to retrieve it from the file in my code.

Does anybody know how to do this and would care to give me a hand?

Thanks a lot

best regards


Derron(Posted 2016) [#2]
ReadStream()
WriteStream()
OpenStream()

stream.WriteString()
stream.WriteInt()
...


Hopefully this are enough keywords to make your search results containing something useful.


A potential hit in google might be this:
https://en.wikibooks.org/wiki/BlitzMax/Modules/Streams/Streams


bye
Ron


Nennig(Posted 2016) [#3]
Hi Ron,

Thanks a lot for your help.
I came up with these lines of codes. They give me the correct sequences of Hexadecimal values when I compare with my file (opened in an Hex editor online).
This binary file was generated from Excel VBA.

How would I put all of this data back into a string?
Sorry for the newbish questions...binary files are new territory to me.

Thank you for your help.
Best regards


filename:TStream=ReadStream("chanson.strum")

While Not Eof(filename)
  
	Print Hex(ReadByte(filename))
	
Wend



Fielder(Posted 2016) [#4]
filename:TStream=ReadStream("chanson.strum")

While Not Eof(filename)
  
	strin$:+chr(ReadByte(filename))
	
Wend
print strin

or if you like a very long sequence of numeric values...

strin$:+ReadByte(filename)



xlsior(Posted 2016) [#5]
Just be aware: reading a byte at a time is a LOT slower than reading a string at a time, so unless you really need the single byte you may want to skip that.

(Alternatively: you can also use LoadBank to load an entire file into a memory bank, and then parse that a byte at a time if needed -- faster than telling the hard drive to load a byte at a time.)


Nennig(Posted 2016) [#6]
Guys, Thanks a lot for your help!

I decided to drop the RC4 encryption for the Vernam encryption algorithm that I found on this site.

It works like a charm! Many thanks for helping me making progress.

Until next time. ;-)

Cheers