Jump to content




[N00b] - Trouble using IO library


7 replies to this topic

#1 Zepate

  • Members
  • 3 posts

Posted 01 June 2018 - 12: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:
Posted Image
https://pasteboard.co/HnSwNkY.png

if i go back to craft os i have the folowing:
Posted Image
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 :)

#2 Bomb Bloke

    Hobbyist Coder

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

Posted 01 June 2018 - 02: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 by Bomb Bloke, 01 June 2018 - 02:00 PM.


#3 Zepate

  • Members
  • 3 posts

Posted 01 June 2018 - 02: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

#4 Janne

  • Members
  • 4 posts

Posted 02 June 2018 - 03:50 PM

View PostZepate, on 01 June 2018 - 02:36 PM, said:

  • 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/m...manual.html#5.7

View PostZepate, on 01 June 2018 - 02:36 PM, said:

  • 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.

#5 Zepate

  • Members
  • 3 posts

Posted 04 June 2018 - 09: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

#6 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 04 June 2018 - 03:31 PM

In the case of CC I'm pretty sure IO is defined in terms of FS. Not the other way around.

#7 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 04 June 2018 - 09:10 PM

https://github.com/d...rom/apis/io.lua

IO is literally a wrapper for fs.

#8 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 04 June 2018 - 09: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()






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users