Jump to content




Writing to binary from a string


  • You cannot reply to this topic
2 replies to this topic

#1 BigSHinyToys

  • Members
  • 1,001 posts

Posted 06 September 2012 - 06: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.

#2 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 06 September 2012 - 06: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.

#3 BigSHinyToys

  • Members
  • 1,001 posts

Posted 07 September 2012 - 03: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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users