Jump to content




Jumping to bytes in binary read/write mode


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

#1 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 14 October 2016 - 12: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 by Geforce Fan, 14 October 2016 - 01:22 AM.


#2 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 14 October 2016 - 11:24 AM

is the io api able to do this? I think it can

#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 14 October 2016 - 01:01 PM

Normally yes, but not within ComputerCraft.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users