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.
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.
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
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()
And fs.writeLine() works too?
local fileHandle = fs.open("/path/to/file", "w") -- Open /path/to/file with write permissions
local fileHandle = fs.open("/path/to/file", "a") -- Open /path/to/file with append permissions
No. Given this lineNo, contents will be a string containing the entire file.
only the first line of the file would be in contents, you need to use readAll to have all the contents.local contents = fileHandle.read()