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

readAll()

Started by Kizz, 02 April 2015 - 11:51 AM
Kizz #1
Posted 02 April 2015 - 01:51 PM
Hey Guys,

So I'm trying to make a log that logs when a specific user logs into my OS. I can make it write fine but it seems to throw an error no matter what I do when I perform a readAll().

I can't find much info in the tutorial and wiki about using readAll().

Here is my code:


   if fs.exists("log") then
	   local h = fs.open("log", "a") -- should I be using read or write mode?
	   loglen=textutils.unserialize(h.readAll()) --error is for this line, with or without unserialize, it throws "Attempt to index ? a nil value"
	   print(loglen) --debugging
	  
	   if string.len(loglen)>30 then --if the length of the file is X bits, delete and recreate to clear all
		   h.close()
		   fs.delete("log")
	   end
	  
	   h.close() -force close to prevent loss of data. not needed
	   local h = fs.open("log", "a") --open to add new log
	   h.write(userName.." - Day: "..os.day().." | Time: "..os.time()..". \n") --userName is derived above this function
	   h.close()
  end


It seems like no matter what I try, I still get an error relating to a nil value. I have forced data into the file and it's still nil. Is it because I am amending the file rather than opening in write or read mode?
Edited on 02 April 2015 - 11:53 AM
KingofGamesYami #2
Posted 02 April 2015 - 02:14 PM
For .readLine or .readAll you should open the file in "r" mode. For adding to the file, open it in "a" mode.

If you don't want to constantly open/close files, you can use .flush instead of .close, however the file will not be available to any other programs if you do so (could cause problems if the script crashes).
CrazedProgrammer #3
Posted 02 April 2015 - 02:16 PM
You used fs.open("log", "a"), where a stands for append (write without overwriting).
You should be using fs.open("log", "r").
Also, textutils.unserialize only takes a string with the format from textutils.serialize.
Fixed code:

if fs.exists("log") then
  local h = fs.open("log", "r")
  loglen=h.readAll()
  h.close()
  print(loglen) --debugging
  if #loglen>30 then --if the length of the file is X bytes, delete and recreate to clear all
    fs.delete("log")
  end
  h = fs.open("log", "a") --open to add new log
  h.writeLine(userName.." - Day: "..os.day().." | Time: "..os.time()..".") --userName is derived above this function
  h.close()
end
Edited on 02 April 2015 - 12:26 PM
Kizz #4
Posted 02 April 2015 - 02:27 PM
Great, I figured it was that. I am just unable to test it due to the slowness of this laptop :(/>.

Thanks for the help guys!
DannySMc #5
Posted 02 April 2015 - 04:54 PM
Great, I figured it was that. I am just unable to test it due to the slowness of this laptop :(/>/>.

Thanks for the help guys!

try using an emulator?
minebuild02 #6
Posted 02 April 2015 - 05:02 PM
Great, I figured it was that. I am just unable to test it due to the slowness of this laptop :(/>/>.

Thanks for the help guys!

try using an emulator?
I recommend CCLite. Or CCEmuRedux.
HPWebcamAble #7
Posted 02 April 2015 - 11:44 PM
Here's the topic for CCEmuRedux, which is my personal favorite:
http://www.computercraft.info/forums2/index.php?/topic/18789-ccemuredux-development/