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

How to access the Windows file system from the UI?

Started by Boozha, 21 February 2013 - 05:10 AM
Boozha #1
Posted 21 February 2013 - 06:10 AM
Title: How to access the Windows file system from the UI?

I lurked quite a bit and searched for the answer to my problems, but I found none that was explained easy enough for such a coding noob as me.

My main problem currently is that I want to create a file in a directory and store variables in that file, and retrieve them later. I checked the wiki and many threads here, but heavily integrated code and tons of other functions wrapped in the same program are making my head spin.

So what I really need is:

- How do I access a file via the integrated API? How does the path have to look?
- How do I edit it automatically?
Lyqyd #2
Posted 21 February 2013 - 06:40 AM
Split into new topic.

The file system of the computers is sandboxed so that they cannot access any folders above their individual computer folder. You can use the io and fs libraries to read from and write to files, and the fs library includes all of the file manipulation calls (such as copy, move, etc.).
Boozha #3
Posted 21 February 2013 - 07:36 AM
I have no coding background and am kind of confused by these commands the wiki led me to: http://www.lua.org/m...manual.html#5.7

So far I only did turtle programs using only basic commands, so delving deeper now is really hard for me.

How does it access the file? Do I have to open it somehow? Where does it write things? How to find and retrieve them afterwards?

I got the fs part working now, I can find and copy files, and presumably open them, but what then?

Edit: managed to write a line in it now (found a program I could rip off from):

vars = fs.open("/disk/varsave", "w")
vars.write("banana")
vars.close()
LBPHacker #4
Posted 21 February 2013 - 09:09 AM
If you want to store variables, first of all, you'll need a table. (Of course you don't, but I like doing this this way.)
 local config = {} -- or whatever
-- Now fill the table with your variables
config.myVar = 3.14 -- this can be a string as well as a number, but it can't be a function or a thread
-- Now you have a table full with arguments. Let's serialize it.
local serializedConfig = textutils.serialize(config) -- textutils.serialize is a pretty neat table-serializer in textutils API
-- serializedConfig is a string that now contains the config table.

-- Open the output file...
local output = fs.open("myFile", "w")
-- If you're playing safe, you can do this:
local output = assert(fs.open("myFile", "w"), "Couldn't save config")
-- Write serializedConfig...
output.write(serializedConfig)
-- Close the file...
output.close()
-- And you're done! 

Now load it.

-- Open the input...
local input = assert(fs.open("myFile", "r"), "Couldn't load config")
-- Get the contents...
local serializedConfig = input.readAll()
-- Get rid of the handle...
input.close()
-- Unserialize the serialized table...
local config = textutils.unserialize(serializedConfig)
-- Enjoy!
print(config.myVar) -- Gives you 3.14
LBPHacker #5
Posted 21 February 2013 - 09:30 AM
Sorry, I didn't answer your real questions…

- How do I access a file via the integrated API? How does the path have to look?
I'm not sure but the path is more like Unix then anything else. So what is "\folder\subfolder\myfile" in Windows, it's "folder/subfolder/myfile" here.

How to access a file? You can use either the IO or the FS API (don't get me wrong, Lua is case-sensitive, and something like FS.open would make it throw "attempt to index nil"s like mad…) I prefer using FS API, only because it's easier, and it's implemented for ComputerCraft. You can open a file in three two modes, for reading and for writing. The second argument passed to fs.open is the mode, so "w" means "Hey, FS, open this file in write mode!", and, of course, "r" means read mode. There is a third mode, called append mode, which is something like write mode. The only difference is, while in write mode, the original content of the output file gets lost, append continues writing the file where it had finished writing last time.

You can open files in binary mode with adding a "b" to the mode argument. So binary write mode is "wb", binary read mode is "rb", and binary append mode is "ab".

As I mentioned before, when you call fs.open, you get a file handle. The methods of this handle are the following:

In read mode:

handle.readLine: Reads and returns the next line of the file. Returns nil if no lines left.
handle.readAll: Reads the whole file, and returns it. This method doesn't exist in binary mode.
handle.read (only in binary mode): Reads only ONE byte and returns it. (The return value is a number between 0 and 255.)

In write or append mode:

handle.writeLine: Writes text into a new line in the file.
handle.write: Writes text to the file.
handle.write (in binary mode): Writed only ONE byte to the file. (The argument must be a number between 0 and 255.)

And of course there is handle.close, but that's self-explaining.

You can learn more here.
LBPHacker #6
Posted 21 February 2013 - 09:31 AM
- How do I edit it automatically?

I have no idea what do you mean by that…
Boozha #7
Posted 21 February 2013 - 10:16 AM
Thanks a bunch, I now got this thingy:

function test3 ()
vars = fs.open("/disk/varsave", "w")
vars.write("banana")
vars.close()
vars = fs.open("/disk/varsave", "r")
var = vars.readAll()
print(var)
end

Its good enough to do what I want to, but I guess a table would do better. How do I store and retrieve multiple variables in that? For me it looks right now as if only one had been stored in it.

Edit: Also, how to do line breaks in a file you want to fill with strings?

Edit two: Or is myVar actually only one space in the table?

Edit three: Got it all working, thanks a lot for the help, you really explained that way better than most tutorials I've seen here ^^
remiX #8
Posted 21 February 2013 - 05:30 PM
Did you look at this page?
Boozha #9
Posted 22 February 2013 - 01:36 AM
Nope, not so far, thanks for the link! now I also know how to do line breaks :P/>