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

Jumping to bytes in binary read/write mode

Started by Geforce Fan, 13 October 2016 - 11:51 PM
Geforce Fan #1
Posted 14 October 2016 - 01:51 AM
When you open a file in wb or rb mode, you can only write at the end of the file, or read byte after byte. Quite simply, my suggestion is to add the ability to jump to certain bytes. For example:

local file = fs.open("file","rb")
file.setPos(256) --# start reading at the 256th byte in the file
local byte256 = file.read()
file.setPos(512) --#start reading at the 512th byte in the file
local byte512 = file.read()
file.close()
and

local file = fs.open("file","wb")
file.setPos(256) --#start writing at the 256th byte in the file or, if the file is shorter than 256 bytes, throw an error
file.write(127)--#The 256th byte is now 127. The old 256th byte has been replaced.
file.close()

This would be useful in scenarios where you only need to access some of the data in a particularly large file, or need to overwrite a section of a file rather than the whole thing.

it's also particularly useful if you want to RAID floppies in a custom file system that is split up into a single file on each drive
Edited on 14 October 2016 - 12:22 AM
Lupus590 #2
Posted 14 October 2016 - 12:24 PM
is the io api able to do this? I think it can
Bomb Bloke #3
Posted 14 October 2016 - 02:01 PM
Normally yes, but not within ComputerCraft.