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

[Lua][Question]Files..

Started by stabby, 03 January 2013 - 04:25 AM
stabby #1
Posted 03 January 2013 - 05:25 AM
So yeah, I've read like 20 of these post but i still can't figure it out..

It's really simple all i want to do is:

Create a file (or write to a previous one).
Write some text in the file ( Like "Message from XXXXX. Message: … ")
Then read the file, put it in a variable and display it on an monitor etc..


I know i'm supposed to use io.open and whatnot. But i can't even write to a file and then display it.
any help? :)/>

Thanks in advance!
OmegaVest #2
Posted 03 January 2013 - 05:39 AM
Okay, so most of this will come from the fs api, on the wiki. Paraphrased, of course, to suit your questions.



file = fs.open(path, "w")  -- The w is for WRITE mode.
file.write(string)  --  Writes a string to the file. add Line to the function name, and it will add a line-break at the end for you.
file.close()  --  Make sure you close your files, so they will save/release.

file = fs.open(path, "r")  -- The r is for READ mode.
rFile = file.read()  -- Reads the whole file to a table. From here, you can do whatever you want with it, supposing you know your tables. If not, go google Lua Manual.
file.close()



So, go check out the wiki, specifically the term, and peripheral apis, and the Lua manual. they will be great helps here.
stabby #3
Posted 03 January 2013 - 06:16 AM
Okay, so most of this will come from the fs api, on the wiki. Paraphrased, of course, to suit your questions.



file = fs.open(path, "w")  -- The w is for WRITE mode.
file.write(string)  --  Writes a string to the file. add Line to the function name, and it will add a line-break at the end for you.
file.close()  --  Make sure you close your files, so they will save/release.

file = fs.open(path, "r")  -- The r is for READ mode.
rFile = file.read()  -- Reads the whole file to a table. From here, you can do whatever you want with it, supposing you know your tables. If not, go google Lua Manual.
file.close()



So, go check out the wiki, specifically the term, and peripheral apis, and the Lua manual. they will be great helps here.


Thanks! It works. Only problem.. How do i use the rFile? Sorry for stupid question i'm just new to files and stuff.

So i can't get it to work with this code:

-- This is the writing code
path = "test"
string = "RandomShit"
file = fs.open(path, "a")
file.write(string)
file.close

-- This is the reading code
path = "test"
file = fs.open(path, "r")
t = file.read()
file.close
print(t)

Thansk again!
Lyqyd #4
Posted 03 January 2013 - 06:38 AM
Don't use string as a variable.
remiX #5
Posted 03 January 2013 - 07:50 AM
Thanks! It works. Only problem.. How do i use the rFile? Sorry for stupid question i'm just new to files and stuff.

So i can't get it to work with this code:

-- This is the writing code
path = "test"
string = "RandomShit"
file = fs.open(path, "a")
file.write(string)
file.close

-- This is the reading code
path = "test"
file = fs.open(path, "r")
t = file.read()
file.close
print(t)

Thansk again!

If you're using the file for just one line, use file.readLine(), it will not return a table then.
And as Lyqyd said, don't use string as a variable because you will overwrite the string API

If you don't know already:
Opening a file in 'a' (append mode) will append text to the file,
Opening in 'w' (write mode) will overwrite all text in the file
stabby #6
Posted 03 January 2013 - 09:51 AM
Thanks guys! It works now! :)/>