Posted 21 July 2013 - 09:07 PM
Hey,
I've peeked around trying to find a way to read and then write a binary file all at once without corruption to no avail. My predicament is this:
Currently, opening a file using io.open(filename, "rb") will work (opens as binary read mode), but using filehandle.read("*a") doesn't seem to read the entire file. Instead, it seems to return just the first byte.
If opening the file with io.open(filename, "r"), the statement filehandle.read("*a") will return a string of the entire file's contents. The problem is that the content becomes corrupt when storing in a string.
Case in point:
The above code makes a corrupted copy of the gif file. When comparing the two files in a hex editor, it isn't in any specific spot (at the beginning or end). The file has corrupted values all throughout.
I considered reading the binary file byte by byte, but that stalls the program quickly and causes it to crash with a file of any real size. So to reiterate; is there any way to read and then write a raw binary file all at once?
I've peeked around trying to find a way to read and then write a binary file all at once without corruption to no avail. My predicament is this:
Currently, opening a file using io.open(filename, "rb") will work (opens as binary read mode), but using filehandle.read("*a") doesn't seem to read the entire file. Instead, it seems to return just the first byte.
If opening the file with io.open(filename, "r"), the statement filehandle.read("*a") will return a string of the entire file's contents. The problem is that the content becomes corrupt when storing in a string.
Case in point:
fh=io.open("/1.gif","r")
r=fh:read("*a")
fh:close()
fh2=io.open("/2.gif","w")
fh2:write(r)
fh2:close()
The above code makes a corrupted copy of the gif file. When comparing the two files in a hex editor, it isn't in any specific spot (at the beginning or end). The file has corrupted values all throughout.
I considered reading the binary file byte by byte, but that stalls the program quickly and causes it to crash with a file of any real size. So to reiterate; is there any way to read and then write a raw binary file all at once?