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

[N00b] - Trouble using IO library

Started by Zepate, 01 June 2018 - 11:47 AM
Zepate #1
Posted 01 June 2018 - 01:47 PM
Hello, i'm new to this forum and I am nice to meet you all.

I have trouble in creating files that i can read with CraftOS. For some reason I can write whatever i want without error but nothing is writen in the intented file.

to begin i use:
  • minecraft 1.12.2
  • MCP (coderpack) 9.19
  • Forge 14.21.1
  • chicken chunk 2.4.1.71
and
  • CC 1.80 pr1
What I did:
  • Build computer, and start it by right clicking then:

https://pasteboard.co/HnSwNkY.png

if i go back to craft os i have the folowing:

https://pasteboard.co/HnSxrAS.png

If i edit the test.txt with "edit test.txt" nothing is inside the file

Can someone help me please :)/>
Bomb Bloke #2
Posted 01 June 2018 - 03:00 PM
You need to use colon notation for all function calls into io-opened file handle tables, not just for "close":

f = io.open("test.txt", "w")
f:write("testing io")
f:close()

Note that this is different to the fs API, which operates in a similar manner but doesn't use colon notation at all:

f = fs.open("test.txt", "w")
f.write("testing fs")
f.close()
Edited on 01 June 2018 - 01:00 PM
Zepate #3
Posted 01 June 2018 - 03:36 PM
Thanks a lot but you will hate me since i found the solution before the approval of the modo 😬 :grimacing:

I think the Wiki must be updated since:
  • there is no tutorial on how to write files from lua in CC
  • the IO api is not enought documented
  • the use of fs api provide missleading coding style with IO api (note the notation "." and ":" for the same purpose)
So if somone have the same issue than me i advise you to do the following.

you want to write some strings to a file. To do so you have 2 option:
  • the hard way but more flexible way is to use IO library
  • the quick and dirty yet powerfull way is to use FS library (CC endemic not sure if it is CraftOS endemic)
What you have to do is to create a file handle that ack like C type file handle:

Myownfile = io.open("myfile.txt","w")
Myownfile.write(Myownfile,"This is the first thing in my file")
Myownfile.close(Myownfile)
This code should do the trick but you can use less verbose and more efficient? notation

f = io.open("text_file.txt","w")
f:write("this is a test")
f:close()

Note the diference between dot notation "." and semicolon ":"

Thanks bomb bloke for your help !

I made a long post and i sure that i left some misspelling since english isn't my native language so if you notice something that hurt your eyes tell me i'll edit
Janne #4
Posted 02 June 2018 - 04:50 PM
  • there is no tutorial on how to write files from lua in CC
  • the IO api is not enought documented
  • the use of fs api provide missleading coding style with IO api (note the notation "." and ":" for the same purpose)
The IO api is just not documented on the wiki, but the wiki does link to the official lua documentation for the IO library http://www.lua.org/manual/5.1/manual.html#5.7

  • the hard way but more flexible way is to use IO library
  • the quick and dirty yet powerfull way is to use FS library (CC endemic not sure if it is CraftOS endemic)
The IO library is just a wrapper around the fs library, I don't quite see how the IO library is "harder" and\or more "flexible" when it has less features than fs.
Zepate #5
Posted 04 June 2018 - 10:28 AM
io is more flexible since it let's you chose the of the characrter of end of line. FS library is made on top of IO so it make sens for me to say io is more flexible but it's my opinion
H4X0RZ #6
Posted 04 June 2018 - 04:31 PM
In the case of CC I'm pretty sure IO is defined in terms of FS. Not the other way around.
KingofGamesYami #7
Posted 04 June 2018 - 10:10 PM
https://github.com/dan200/ComputerCraft/blob/master/src/main/resources/assets/computercraft/lua/rom/apis/io.lua

IO is literally a wrapper for fs.
SquidDev #8
Posted 04 June 2018 - 10:14 PM
To be fair, there are some rather powerful things you can do with the io library that the fs library doesn't support as well:

--# Easy iteration over lines with automatic closing
for line in io.lines "my_file" do print(line) end

--# Multiple reads in a single call (though not supported by CC (yet))
local handle = io.open("something")
local part1, line, rest = handle:read(5, "l", "a")
handle:close()