Here is a little simple guide how to read / write / append to files
-------------------------------------------------------------------------------
-- Lua 5 File Handling Guide / By NightLitch
-------------------------------------------------------------------------------
--// Simple Reading File
function ReadFile(filename)
io.input(filename)
for line in io:lines() do
-- todo code here
end
io.input()
end
--// Simple Create File and Write:
function CreateFile(filename)
io.output(filename)
io.write("what ever you want to be saved")
io.output()
end
-- Diff. Mode functions
--// Append mode
function AppendToFile(filename)
local file = io.open(filename, "a+") -- "a+"
file:write("appending this text to file "..filename.."\r\n")
file:write("continuing appending / writing text to file\r\n")
file:close()
end
--// Read mode
function ReadFromFile(filename)
local file = io.open(filename, "r") -- "r" read
for line in file:lines() do
-- todo code here
end
file:close()
end
--// Write mode
function WriteToFile(filename)
local file = io.open(filename, "w+") -- "w" write
file:write("write stuff to file...")
file:close()
end
-- reference:
local file_to_read = io.input(filename) -- file is the file handler
for line in file_to_read:lines() do -- use created file handler to lines()
end
file_to_read:close() -- use created file handler to close file
local file_to_write = io.output(filename) -- file is the file handler
file_to_write:write("string to write") -- use created file handler to write
file_to_write:close() -- use created file handler to close file
Cheers / NL
very hlpfl nl .. good going .. ;)
Thx, NL, you made my white areas colourful. :)
" i was looking but this, but i forgot to look here.. "
almost posted a request for this how to ..
so thanks again..
function savefile(table,file)
[color=#FF0000]-- io.output(file)
-- for a,b in table do
-- io.write(a.."\n")
-- end
-- io.output()[/color]
local f=io.open(file,"w")
for a,b in table do
f:write(a.."\n")
end
f:close()
end
The red part did not write content to files that were already created. Is this normal?
function savefile(table,file)
io.output(file)
for a,b in table do
io.write(a.."\n")
[COLOR=blue] io.close()[/COLOR]
end
io.output()
end
indeed its normal when u forget to close the file, i.e add the line in blue
QuoteOriginally posted by Dessamator
function savefile(table,file)
io.output(file)
for a,b in table do
io.write(a.."\n")
[COLOR=blue] io.close()[/COLOR]
end
io.output()
end
indeed its normal when u forget to close the file, i.e add the line in blue
Damn, ok thank you, I haven't done scripting for 2 months and never knew LUA5 very well... thx for helping me out... :)
ur welcome, i dont know lua 5 that well either, just learnt it by "trial and error":)