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

How to write a binary file

Started by Seeker14491, 26 May 2014 - 08:21 AM
Seeker14491 #1
Posted 26 May 2014 - 10:21 AM
I'm stumped, how do I go about writing a binary file? All I get is an empty file with this code:

f = fs.open("file", "wb")
f:write(71)
f:write(79)
f:close()
print(fs.getSize"file")
Outputs: 0
theoriginalbit #2
Posted 26 May 2014 - 10:43 AM
firstly fs uses dot notation not colon syntax, so you should be

local f = fs.open("file", "wb")
f.write(71)
f.write(79)
f.close()
print(fs.getSize("file"))

however that code works perfectly fine for me, outputting the file size of 512 (which is kinda incorrect, read the opening paragraph here). What version of ComputerCraft are you using? are you using an emulator?
Seeker14491 #3
Posted 26 May 2014 - 06:13 PM
firstly fs uses dot notation not colon syntax, so you should be

local f = fs.open("file", "wb")
f.write(71)
f.write(79)
f.close()
print(fs.getSize("file"))

however that code works perfectly fine for me, outputting the file size of 512 (which is kinda incorrect, read the opening paragraph here). What version of ComputerCraft are you using? are you using an emulator?
I'm on ComputerCraft 1.63, no other mods. Indeed, the dot syntax works, and the program now prints 2, though contrary to what the wiki says.
Edited on 26 May 2014 - 04:14 PM
MKlegoman357 #4
Posted 26 May 2014 - 08:41 PM
I'm on ComputerCraft 1.63, no other mods. Indeed, the dot syntax works, and the program now prints 2, though contrary to what the wiki says.

Where have you read that you need to use colon notation instead of dot notation on handles returned by fs.open? io.open returns a handle which uses colon notation.
Lignum #5
Posted 26 May 2014 - 09:15 PM
and the program now prints 2, though contrary to what the wiki says.
Are you using an emulator? Because some emulators might output the file size correctly.
Seeker14491 #6
Posted 26 May 2014 - 09:40 PM
I'm on ComputerCraft 1.63, no other mods. Indeed, the dot syntax works, and the program now prints 2, though contrary to what the wiki says.

Where have you read that you need to use colon notation instead of dot notation on handles returned by fs.open? io.open returns a handle which uses colon notation.
Nowhere, I was indeed getting confused with io.open. I understand it now, I haven't used the fs api too much.

and the program now prints 2, though contrary to what the wiki says.
Are you using an emulator? Because some emulators might output the file size correctly.
No emulator, Vanilla Minecraft 1.6.4 with ComputerCraft 1.63, absolutely no other mods.
theoriginalbit #7
Posted 27 May 2014 - 01:42 AM
and the program now prints 2, though contrary to what the wiki says.
Are you using an emulator? Because some emulators might output the file size correctly.
No emulator, Vanilla Minecraft 1.6.4 with ComputerCraft 1.63, absolutely no other mods.
perhaps dan200 has finally fixed that 'bug'