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

Reading a specific line with in a file

Started by MayContainVennom, 21 February 2014 - 12:11 PM
MayContainVennom #1
Posted 21 February 2014 - 01:11 PM
Hello there,

I'be been looking around here and had a few test runs but still can't work it out. I need a function that can read one line from a file when called (For example readLine(filename,line). I found one thread that seemed to have it working but couldn't get it working on my end. If you are still unsure, If I have a file set up like this:
Line 1
Line 2

I want to be able to read this file and retrieve what is on the second line. If you could give me a hand with this I would be very thankful as it is a start to a large project of mine…

Thanks,
Vennom
CometWolf #2
Posted 21 February 2014 - 01:26 PM
Use the fs api
http://computercraft.info/wiki/Fs_%28API%29

local file = fs.open("fileName","mode") -- mode in this case should be "r"
file.readLine() --reads first line
variableYouWant = file.readLine() -reads second line
file.close -- close the file handle
MayContainVennom #3
Posted 21 February 2014 - 01:45 PM
Use the fs api
http://computercraft...ki/Fs_%28API%29

local file = fs.open("fileName","mode") -- mode in this case should be "r"
file.readLine() --reads first line
variableYouWant = file.readLine() -reads second line
file.close -- close the file handle

This would work for that example but I wont be just calling for line 2. In the final version I will wanting to be calling 1 put of a possible 10 lines per computer, The example's I have seen turn it into a table then pull the line but I haven't been able to get them to work.
CometWolf #4
Posted 21 February 2014 - 02:01 PM
It's quite literally the exact same thing, you just insert the result into a table.

local file = fs.open(you get the idea)
local tLines = {}
local line = file.readLine()
while line do
  tLines[#tLines+1] = line
  line = file.readLine()
end
file.close()
variableYouWant = tLines[2]
Edited on 21 February 2014 - 01:03 PM
Bomb Bloke #5
Posted 21 February 2014 - 07:38 PM
If you're still having trouble, I suggest posting the code you're trying to use.