PtokaX forum

Archive => HOW-TO's => Archived 5.1 boards => Old HOW-TO's => Topic started by: NightLitch on 04 March, 2005, 17:30:08

Title: File handling
Post by: NightLitch on 04 March, 2005, 17:30:08
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
Title:
Post by: Herodes on 04 March, 2005, 17:42:47
very hlpfl nl .. good going .. ;)
Title:
Post by: bastya_elvtars on 04 March, 2005, 22:12:25
Thx, NL, you made my white areas colourful. :)
Title: thanks, great !!
Post by: UwV on 04 March, 2005, 22:37:29
" i was looking but this, but i forgot to look here.. "
  almost posted a request for this how to ..

 so thanks again..
Title:
Post by: bastya_elvtars on 31 May, 2005, 10:41:05
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?
Title:
Post by: Dessamator on 31 May, 2005, 10:59:39
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
Title:
Post by: bastya_elvtars on 31 May, 2005, 11:35:27
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... :)
Title:
Post by: Dessamator on 31 May, 2005, 23:31:17
ur welcome, i dont know lua 5 that well either, just learnt it by "trial and error":)