99 posts
Location
Louisville, Kentucky
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
3057 posts
Location
United States of America
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).
546 posts
Location
Wageningen, The Netherlands
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
99 posts
Location
Louisville, Kentucky
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!
1847 posts
Location
/home/dannysmc95
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?
100 posts
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.
957 posts
Location
Web Development
Posted 02 April 2015 - 11:44 PM