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

Help With Fs Api

Started by grand_mind1, 10 September 2013 - 06:58 PM
grand_mind1 #1
Posted 10 September 2013 - 08:58 PM
I want to make a program that will read from a file and make a list using what is on that file. So if I had this on the file:

test
test1
test2
it would print out:

>test
>test1
>test2
So I have two problems that are caused by each other. I want to add that little ">" to the front in a different than the actual words. I could just add it to the beginning of the words in the file it reads from but I don't know how I could get it to be a different color then. I think I want to use fs.readAll() because I want to be able to add as many things to the list as I want, so I can't just turn them all into different variables using fs.readLine() and add the ">" to them.
I have no idea how I can do this.
Help is appreciated!
Thanks! :D/>
Lyqyd #2
Posted 10 September 2013 - 09:33 PM
Of course you can use .readLine(), or the :lines() iterator if it's an IO file handle (like it should be! :P/> ). You just need to read the items in to a table, then print out however many of them you want with whatever other colors/decorations/etc. that you want.

Please post your current code.
grand_mind1 #3
Posted 10 September 2013 - 09:42 PM
Sorry, but I don't really have much of anything unless you count opening and closing the file with the fs API. The rest of the program would be printing out the list with the information it got from reading the file. I guess this is what I have though:

test = fs.open("test","r")
test.readAll()
--I need to somehow print out what it read from the file
test.close()
And I have this on the "test" file:

test
test1
test2
Lyqyd #4
Posted 10 September 2013 - 09:51 PM
So, you'd do something vaguely thus:


local linesTable = {}
local handle = io.open("test", "r")
if handle then
  for line in handle:lines() do
    --put each line of the file in the table.
    table.insert(linesTable, line)
  end
  handle:close()
end

for i = 1, #linesTable do
  term.setTextColor(colors.lime)
  term.write(">")
  term.setTextColor(colors.white)
  print(linesTable[i])
end

Notice that we read every line individually in to a table, the iterate through that table, write the prefix on to the screen, then print the whole line, using a different color for the prefix and the line.
grand_mind1 #5
Posted 10 September 2013 - 10:21 PM
Could you explain two things from your code to me?
First, are io and fs like the same? Could I use fs instead of io or vice versa in this code?
Second, does the ":lines()" thing just return the amount of lines in the program that was opened?

Also, when I try to put this part:

local linesTable = {}
local handle = io.open("test", "r")
if handle then
  for line in handle:lines() do
	--put each line of the file in the table.
	table.insert(linesTable, line)
  end
  handle:close()
end
in a function and call it before running the second part where it prints everything out, it gives me this error:

test:14 attempt to get length of nil
I'm using this code:

local function test()
  local linesTable = {}
  local handle = io.open("test", "r")
  if handle then
	for line in handle:lines() do
	  table.insert(linesTable, line)
	end
	handle:close()
  end
end
test()
for i = 1, #linesTable do
  term.setTextColor(colors.lime)
  term.write(">")
  term.setTextColor(colors.white)
  print(linesTable[i])
end
PixelToast #6
Posted 10 September 2013 - 10:42 PM
file:lines() will return an iterator for the for loop (similarly how the pairs function works)

io is different from fs, it is a Lua api and its uses are here: http://www.lua.org/manual/5.1/manual.html#5.7

fs is here: http://computercraft.info/wiki/Fs_(API)

the main difference though, is that the file handler outputted from fs does not use a colon : and a . instead

local file=fs.open("derp","w")
file.write(stuff)
file.close()
local file=io.open("derp","w")
file:write(stuff)
file:close()
Lyqyd #7
Posted 10 September 2013 - 11:13 PM
You can't just put part of the code into a function and expect it to keep working. Look, the linesTable is declared as local, but you've put that in a function, so now it's local to the function. Of course it won't be available after the function ends. Try using the *exact* code I posted, or have the function return the table of lines or something.
grand_mind1 #8
Posted 11 September 2013 - 07:34 PM
Oh! I didn't know that you could have a variable that was local to just a function.