File handling
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

File handling

Started by NightLitch, 04 March, 2005, 17:30:08

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

NightLitch

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
//NL

Herodes

very hlpfl nl .. good going .. ;)

bastya_elvtars

Thx, NL, you made my white areas colourful. :)
Everything could have been anything else and it would have just as much meaning.

UwV

" i was looking but this, but i forgot to look here.. "
  almost posted a request for this how to ..

 so thanks again..
\NL   The knowledge and skills you have achieved are meant to be forgotten so you can float comfortably in emptiness, without obstruction.
" Holly loves me,...  . "      ;o)

& don't forget, the motto is :
  -- SUPPORT YOUR LOCAL DJ'S --

bastya_elvtars

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?
Everything could have been anything else and it would have just as much meaning.

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
Ignorance is Bliss.

bastya_elvtars

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... :)
Everything could have been anything else and it would have just as much meaning.

Dessamator

ur welcome, i dont know lua 5 that well either, just learnt it by "trial and error":)
Ignorance is Bliss.

SMF spam blocked by CleanTalk