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

File reading issue

Started by gatt427, 11 March 2015 - 11:50 PM
gatt427 #1
Posted 12 March 2015 - 12:50 AM
Hello guys,
I have a program that is designed as a PM service ingame as part of an OS I am developing. Part of the program receives data collected from another program (the OS) via a file with the data stored in it. I have checked, and the file is generated and populated properly. I just can't seem to read it. When I try to run the program I get the error

mail:61: attempt to index ? (a nil value)
The error is caused in the line:

line = file.readLine()
I know that normally this is caused by misspelling a function or method, but all my spelling seems to be in order.
Here is the code:

--Variables
local vQuit = false
local numOfMessages = 0
local messages = {"m1", "m2", "m3"}
local functionList = {"list", "help", "clear", "quit"}
local functionListReal = {list, help, clear, quit}
local mainPath = "/GoldOS"
local mailPath = mainPath.."/data/mail"
--Startup function (Tell user they have messages)
function startup()
  term.clear()
  rednet.open("back")
  print("Welcome to Mail, you have "..numOfMessages.." new messages.")
  print(" ")
  print("Type 'list' to see a list of messages or 'help' for more commands")
  readIt()
  return
end
--Read user response
function readIt()
  recieve()
  local input = read()
  for i,v in ipairs(functionList) do
	if input==v then
	  functionListReal[i]()
	end
  end
  return
end
--List messages
function list()
  for i,v in ipairs(messages) do
	print(v)
  end
  readIt()
  return
end
--clear screen
function clear()
  term.clear()
  readIt()
  return
end
--Give command help
function help()
  --output some help commands
  print("type 'list' to list messages")
  print("type 'quit' to exit Mail")
  print("type 'clear' to clear previous actions from screen")
  readIt()
  return
end
--Add message to list
function recieve()
  local file = fs.open(mailPath, "r")
  while not line == nil do
	line = file.readLine()
	table.insert(messages, line)
	numOfMessages = numOfMessages+1
  end
  file.close()
end
--quit the mail application
function quit()
  vQuit = true;
  os.reboot()
  return
end
startup()
Thanks in advance,
Gatt427
KingofGamesYami #2
Posted 12 March 2015 - 01:15 AM
The only problem I can see is your while loop will never be executed - line was not predefined. I believe you want something more of this:

local line = "" --#set line to a non-nil value so loop will execute
while line ~= nil do
  line = file.readLine()
  table.insert( messages, lines )
  numOfMessages = numOfMessages + 1
end

Other than that, all I can say is check if that file exists.
Bomb Bloke #3
Posted 12 March 2015 - 02:20 AM
The error is saying that "file" is a nil value (you can't index into it like you can a table). The reason it's nil is because fs.open(mailPath, "r")
(a couple of lines up) didn't return anything. That indicates that either the file specified by "mailPath" doesn't exist, or it otherwise can't be opened.

Assuming it does indeed exist, one possible reason for your problem is that the file is locked by another process. Restarting your computer would be a good start if that's the case.
gatt427 #4
Posted 12 March 2015 - 07:27 PM
So, putting line outside the loop did fix that issue. I had messed with that earlier while trying to fix the real issue. Sorry I missed that.
The actual issue is that the code runs error less, but does not return data from the file. I hand typed a message in /GoldOS/data/mail and it still does not see it when I call the list function