22 posts
Location
California, United States of America
Posted 27 October 2013 - 04:23 PM
I've looked everywhere and I cannot find one good tutorial for this, only bits of code that I can't comprehend. The most helpful source was the Lua website, but it doesn't have a lot of user-friendly information. I mostly learn from example and thorough explaining, and I can't anything that has this. Could anyone please help me with this?
7083 posts
Location
Tasmania (AU)
Posted 27 October 2013 - 05:11 PM
FS APIIn particular, the documentation for
fs.open() deals with what you can do after opening a file.
A short example:
local myFile = fs.open("MyFileName","w") -- myFile can now be used to write to the actual file.
myFile.writeLine("This text was written into \"MyFileName\".")
myFile.close() -- Close file handles when done with them.
myFile = fs.open("MyFileName","r") -- myFile can now be used to read from the actual file.
local myText = myFile.readLine() -- Dumps the first line of text from the file into myText.
myFile.close()
print(myText)
22 posts
Location
California, United States of America
Posted 27 October 2013 - 06:46 PM
FS APIIn particular, the documentation for
fs.open() deals with what you can do after opening a file.
A short example:
local myFile = fs.open("MyFileName","w") -- myFile can now be used to write to the actual file.
myFile.writeLine("This text was written into \"MyFileName\".")
myFile.close() -- Close file handles when done with them.
myFile = fs.open("MyFileName","r") -- myFile can now be used to read from the actual file.
local myText = myFile.readLine() -- Dumps the first line of text from the file into myText.
myFile.close()
print(myText)
Thanks for the little tutorial.
19 posts
Posted 27 October 2013 - 07:20 PM
Easiest way would probably b,e to call the readLine()-function n-times until you reach your desired line.
Here a snippet that should work fine:
function getLine(file, lineno)
local h = fs.open(file, "r")
local line = ""
for i = 1, lineno do
line = h.readLine()
end
h.close()
return line
end
I bet there are more "neat" ways to do this, but the code above does what is should do.