This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
BigSHinyToys's profile picture

Writing to binary from a string

Started by BigSHinyToys, 06 September 2012 - 05:20 PM
BigSHinyToys #1
Posted 06 September 2012 - 07:20 PM
I have been trying to create function for reading and writing binary from a file to a string and back. similar to the readAll function but without changing the data as it is read. the normal readAll causes the File to be corrupted as it is loaded this happened because the binary doesn't match a character so the computer corrects this error and in doing so corrupted data in transit.

to work with raw unaltered data the read ("rb") and write ("wb") binary options can be used with fs.open.

the bellow code will load a file into a string binary and all . my problem in the write function I cant work out how to make it function. It keeps giving an odd error [binReadWrite.lua:27: bad argument: int expected, got table]

I know there are other ways to read and write from a file but for working with MIDI file it has to be binary.

http://pastebin.com/1NbdZpS7

If someone would be so kind as to explain writing binary to a file I would very much appreciate it.
MysticT #2
Posted 06 September 2012 - 07:28 PM
There's two errors:
The first one is that the handles returned by fs.open can't be used with : you need to use . instead:

file:write(something) -- error
file.write(something) -- ok
Second, string.byte takes a string and the position of the character as parameters. So you have to change this line:

file:write(string.byte(string.sub(sDATA,i,i)))
to:

file.write(string.byte(sDATA, i))

That should fix it.
BigSHinyToys #3
Posted 07 September 2012 - 04:30 AM
That worked I had been banging my head against that one for a couple of hours and not making progress.
Thanks it is working finally