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

Fs Api Help

Started by DiabolusNeil, 27 October 2013 - 03:23 PM
DiabolusNeil #1
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?
Bomb Bloke #2
Posted 27 October 2013 - 05:11 PM
FS API

In 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)
DiabolusNeil #3
Posted 27 October 2013 - 06:46 PM
FS API

In 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.
DerKoch #4
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.