CC-DOS
I want to:- Read a specific line in configuration
- Button listener
- Write in a specific line in config
- Draggable window
Posted 22 May 2013 - 11:55 PM
CC-DOS
I want to:Posted 23 May 2013 - 12:16 AM
Posted 23 May 2013 - 12:19 AM
Posted 23 May 2013 - 12:27 AM
Posted 23 May 2013 - 03:33 AM
local function readline(fname,line)
local file = fs.open(fname, "r") -- open the file
if file then --check if file exists
local fContent = file.readAll() --read contents
local fTable = textutils.unserialize(fContent) -- put content to a table (be careful this only works if the content was created by serializing a table
file.close() -- close file handle
return fTable[line] -- return table entry that is equal to the line in the file
else -- if file does not exists
return "file not Found" -- give errormessage
end
end
local function writetoline(fname,insertstring,line) -- with this you create the file line by line.
local file = fs.open(fname, "r") -- open file
if file then -- check if exists
local fContent = file.readAll() -- if exists read content
file.close() -- close file handle
fTable = textutils.unserialize(fContent) -- read content of the file to table
fTable[line] = insertstring -- insert new line or override existing line
else -- if file does not exists
fTable = {insertstring} --create a table with the text you want to add this will be added to line 1 of your file
end
file = fs.open(fname, "w") --open file for saving
file.write(textutils.serialize(fTable)) -- convert table to a string that we can read with unserialize
file.close() -- close filehandle
end
writetoline("test.txt","dies",1) -- write "dies" to line 1
writetoline("test.txt","ist",2) -- write "ist" to line 2
writetoline("test.txt","ein",3)
writetoline("test.txt","beispiel",4)
writetoline("test.txt","Moep",5)
bla = readline("test.txt",5)) -- read line 5
print(bla) -- print the variable
0 members, 2 guests, 0 anonymous users