Posted 27 October 2012 - 04:14 PM
So I was testing with metatables, and then I came up with this.
Instead of messing with io.open, you can just open a file by indexing a table!
for example:
Just put this in front of your program:
You can also run the program once, and try it out in the 'lua' program.
EDIT:
You can now delete a file by setting it to nil
Instead of messing with io.open, you can just open a file by indexing a table!
for example:
print(files.test)
would print the contents of the file 'test'
files.test = "Some text"
Would write "some text" in the file 'test'Just put this in front of your program:
local mt = {}
local function getMagicHandle()
local t = {}
setmetatable(t,mt)
return t
end
mt.__index = function (t,i)
local handle = io.open(i,"r")
if handle then
local s = handle:read("*a")
handle:close()
return s
end
end
mt.__newindex = function(t,i,v)
if type(v)=="nil" then
fs.delete(i)
elseif type(v)=="string" then
local handle = io.open(i,"w")
if handle then
handle:write(v)
handle:close()
end
end
end
files = getMagicHandle()
You can also run the program once, and try it out in the 'lua' program.
EDIT:
You can now delete a file by setting it to nil