Hi!
How to use fs.open() and how to edit files with programs?
Please don't give me links to computercft wiki, because I don't understand the entire cc wiki.
Thanks.
Help me! fs.open
Started by Konlab, May 16 2014 04:30 PM
10 replies to this topic
#1
Posted 16 May 2014 - 04:30 PM
#2
Posted 16 May 2014 - 04:58 PM
local fileHandle = fs.open("/path/to/file", "w") -- Open /path/to/file with write permissions
fileHandle.write("Some Text") -- Replaces the current contents of /path/to/file with "Some Text"
fileHandle.close() -- Stops editing, you should always do this.
#3
Posted 16 May 2014 - 05:04 PM
Thanks very much. And please say me how can I read things.
Also again thanks very much I've waited 1 month ago for this, all people gave only links to wikipedia.
Also again thanks very much I've waited 1 month ago for this, all people gave only links to wikipedia.
#4
Posted 16 May 2014 - 05:07 PM
Sure.
local fileHandle = fs.open("/path/to/file", "r") -- Open /path/to/file with read permissions
local contents = fileHandle.readAll() -- Saves the contents of /path/to/file to the contents variable
fileHandle.close() -- Stops reading
Edited by ZudoHackz, 17 May 2014 - 02:02 PM.
#5
Posted 16 May 2014 - 05:16 PM
And the contents variable will a table?
If table then the indexes are for lines?
Thanks
If table then the indexes are for lines?
Thanks
#6
Posted 16 May 2014 - 05:21 PM
No, contents will be a string containing the entire file. If you wanted to get an individual line, you would need to do something like this.
**WARNING - UNTESTED BAD CODE**
**WARNING - UNTESTED BAD CODE**
local fileHandle = fs.open("/path/to/file", "r") -- Open /path/to/file with read permissions
local lineToGet = 5 -- Which line to read?
local lineContents = "" -- Will contain the contents of that line
for _ = 1, lineToGet do -- Repeats until it reaches the right line.
lineContents = fileHandle.readLine() -- Reads the line.
end
fileHandle.close()
Edited by ZudoHackz, 16 May 2014 - 05:22 PM.
#7
Posted 16 May 2014 - 05:22 PM
Thanks very much :-)
It helped a lot.
Thanks very much
:-) :-)
It helped a lot.
Thanks very much
:-) :-)
#8
Posted 16 May 2014 - 05:23 PM
No problem
#9
Posted 17 May 2014 - 05:57 AM
And fs.writeLine() works too?
#10
Posted 17 May 2014 - 01:20 PM
Konlab, on 17 May 2014 - 05:57 AM, said:
And fs.writeLine() works too?
Do you want to append text onto the end of the file? writeLine() just appends an EOL character to what you entered. If you want to append text, change:
local fileHandle = fs.open("/path/to/file", "w") -- Open /path/to/file with write permissions
to
local fileHandle = fs.open("/path/to/file", "a") -- Open /path/to/file with append permissions
Edited by ZudoHackz, 17 May 2014 - 01:23 PM.
#11
Posted 17 May 2014 - 01:33 PM
ZudoHackz, on 16 May 2014 - 05:21 PM, said:
No, contents will be a string containing the entire file.
ZudoHackz, on 16 May 2014 - 05:07 PM, said:
local contents = fileHandle.read()
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











