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

Choosing A Specifc Line Within A File To Read

Started by MayContainVennom, 18 November 2013 - 02:43 PM
MayContainVennom #1
Posted 18 November 2013 - 03:43 PM
Hello there,

I am working on a code that incorporates the Small Net system from Miscperipherals to read the Last line within a file. The problem I am having is I don't know the fucntion or the way to select a line from that file. I tried file.readLine(x) but that doesn't allow you to select the line it just reads the first one.

Thanks,
Vennom.


function lineCount()
  local file = fs.open("disk/logs", "r")
  if file then
	local i = 0
	while file.readLine() do
	  i = i + 1
	end
	file.close()
	print(i)
	return i
  end
end

function finding()
  line = lineCount()
  print(line.."x")
  local file = fs.open("disk/logs","r")
  local a = file.readLine(line)
  print(a)
  return a
end

function transmiting()
  local Venn = peripheral.wrap("smNetSender_0")
  local Edd = peripheral.wrap("smNetSender_1")
  local p = finding()
  Venn.send("smartHelemet",p)
  Edd.send("smartHelemet",p)
end

while true do
  transmiting()
  sleep(5)
end
Engineer #2
Posted 18 November 2013 - 03:56 PM
A proper way of doing this is reading the whole file and putting it in a table, and with that you can request each line you will need from that file. The code isnt actually really hard:
option 1

local lines = {}
local file = fs.open( "file", "r" )
for line in file.readLine do
	table.insert( lines, line )
end
file.close()
option 2

local lines = {}
local file = fs.open( "file", "r" )
local line = file.readLine()

while line do
	table.insert( lines, line )
	line = file.readLine()
end
file.close()
Those two options do essentially the same. To get the last line, you just get the table of the length and use that as index:

local lastLine = lines[ #lines ]
Edited on 18 November 2013 - 05:33 PM
Bomb Bloke #3
Posted 18 November 2013 - 05:32 PM
If you want to be reading different lines from the file at different times, then odds are Engineer's methods are what you want: Load the whole thing into a table, then pull from there in future.

If you specifically only want the one line, period, then there are other ways you might go about it. Eg:

local bufferline,line="",""
local file = fs.open( "file", "r" )

while line do
  bufferline = line
  line = file.readLine()
end

file.close()
return bufferline

If you're on CC 1.55 or later, it should be possible to see the amount of lines available in the file ("file:lines()") and hence pinpoint the last. Regardless, you still have to iterate through reading all the other lines in the file first.
MayContainVennom #4
Posted 19 November 2013 - 04:26 AM
A proper way of doing this is reading the whole file and putting it in a table, and with that you can request each line you will need from that file. The code isnt actually really hard:
option 1

local lines = {}
local file = fs.open( "file", "r" )
for line in file.readLine do
	table.insert( lines, line )
end
file.close()
option 2

local lines = {}
local file = fs.open( "file", "r" )
local line = file.readLine()

while line do
	table.insert( lines, line )
	line = file.readLine()
end
file.close()
Those two options do essentially the same. To get the last line, you just get the table of the length and use that as index:

local lastLine = lines[ #lines ]


Thanks for your help :D/>