View previous topic :: View next topic |
Author |
Message |
akumakuja28 Master Cheater Reputation: 16
Joined: 28 Jun 2015 Posts: 432
|
Posted: Sun Mar 20, 2016 2:58 am Post subject: Save Array to File with Lines? |
|
|
Code: |
local file2 = io.open("c:\\users\\akuma\\desktop\\lua\\hello.txt","r+")
local read = {} -- alloc?????
local loop = 0 --define start of loop
for line in file2:lines() do
loop = loop + 1 ----read = read line + 1 on loop
read[loop] = line
print(read[loop]) -- print file
end
file2:close() |
Above code read all lines. Easy enough.
Trying to accomplish a simple .ini file of a couple settings.
So the above FILE(hello.txt) has 5 lines in it consisting of...
899 --Screen top pos
1022 -- Screen left pos
566 --form width
700 --form height
1 -- Check for compare
these lines are easy to read and return the postion and size of my window. eg..
Code: |
function loadsavedsettings(timer)
MyForm.Top = read[1]
MyForm.Left = read[2]
MyForm.Width = read[3]
MyForm.Height = read[4]
end
|
What i cant figure out is how to write new settings back after the updated values in the array. eg..
Code: |
function SAVEsettingsOnCLOSE(sender)
read[1] = MyForm.Top
read[2] = MyForm.Left
read[3] = MyForm.Width
read[4] = MyForm.Height
end
local file2 = io.open("c:\\users\\akuma\\desktop\\lua\\hello.txt","r+")
write the array back to file2
end |
How can an Array be written to file LINE by LINE? _________________
|
|
Back to top |
|
|
panraven Grandmaster Cheater Reputation: 55
Joined: 01 Oct 2008 Posts: 942
|
Posted: Sun Mar 20, 2016 6:59 am Post subject: |
|
|
"How can an Array be written to file LINE by LINE? "
The array entry can be 'join' by table.concat function http://www.lua.org/manual/5.3/manual.html#pdf-table.concat . It return a string with each array entry join by a delimiter.
Code: | local s = table.concat(arr,'\n') |
then the string can be write to the file in one go:
Code: |
local f = io.open('save','w')
f:write(s)
f:close()
|
Or do it under a for loop:
Code: |
local f = io.open('save','w')
for i=1,#arr do
f:write(arr[i]..'\n')
end
f:close()
|
The arr entry may need to be string or number for the concatenation work.
CE has a settings class, which store data in user registry, may have a look:
Code: | Settings class
This class can be used to read out and set settings of cheat engine and of plugins, and store your own data |
ADDED:
Steve Donovan's Penlight seems have a self-contained module that work with ini file:
https://github.com/stevedonovan/Penlight/blob/master/lua/pl/config.lua
http://stevedonovan.github.io/Penlight/api/libraries/pl.config.html
...oops, no writing function, sry~ _________________
- Retarded. |
|
Back to top |
|
|
mgr.inz.Player I post too much Reputation: 218
Joined: 07 Nov 2008 Posts: 4438 Location: W kraju nad Wisla. UTC+01:00
|
Posted: Sun Mar 20, 2016 7:59 am Post subject: |
|
|
Code: | filename=[[C:\position.dat]]
function loadSettings()
local settings = createStringlist()
settings.loadFromFile(filename)
MyForm.Top = tonumber(settings[0])
MyForm.Left = tonumber(settings[1])
MyForm.Width = tonumber(settings[2])
MyForm.Height = tonumber(settings[3])
settings.destroy()
end
function saveSettings()
local settings = createStringlist()
settings.add( MyForm.Top )
settings.add( MyForm.Left )
settings.add( MyForm.Width )
settings.add( MyForm.Height )
settings.saveToFile(filename)
settings.destroy()
end |
_________________
|
|
Back to top |
|
|
MikeNoey Advanced Cheater Reputation: 0
Joined: 08 Jun 2018 Posts: 64
|
Posted: Mon Jul 29, 2019 10:38 pm Post subject: |
|
|
Code: | filename=[[C:\config.txt]]
function loadSettings()
local settings = createStringlist()
settings.loadFromFile(filename)
MyForm.Top = tonumber(settings[0])
MyForm.Left = tonumber(settings[1])
MyForm.Width = tonumber(settings[2])
MyForm.Height = tonumber(settings[3])
settings.destroy()
end
function saveSettings()
local settings = createStringlist()
settings.add( MyForm.Top )
settings.add( MyForm.Left )
settings.add( MyForm.Width )
settings.add( MyForm.Height )
settings.saveToFile(filename)
settings.destroy()
end
loadSettings()
|
I get the following error upon executing this script
"Error:[string "filename =[[C:\config.txt]]..."]:7: attempt to index a nil value (global 'MyForm')"
the contents of the config.txt file
899
1022
566
700
What am I doing wrong ? |
|
Back to top |
|
|
Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
Posted: Mon Jul 29, 2019 11:05 pm Post subject: |
|
|
change 'MyForm' to your form name
or change the function to these:
Code: | filename=[[C:\config.txt]]
function loadSettings(MyForm)
local settings = createStringlist()
settings.loadFromFile(filename)
MyForm.Top = tonumber(settings[0])
MyForm.Left = tonumber(settings[1])
MyForm.Width = tonumber(settings[2])
MyForm.Height = tonumber(settings[3])
settings.destroy()
end
function saveSettings(MyForm)
local settings = createStringlist()
settings.add( MyForm.Top )
settings.add( MyForm.Left )
settings.add( MyForm.Width )
settings.add( MyForm.Height )
settings.saveToFile(filename)
settings.destroy()
end
-- use:
saveSettings(UDF1) -- or form name
loadSettings(UDF1) -- or form name
|
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL |
|
Back to top |
|
|
MikeNoey Advanced Cheater Reputation: 0
Joined: 08 Jun 2018 Posts: 64
|
Posted: Wed Jul 31, 2019 10:41 pm Post subject: |
|
|
I figured out why it wasn't working for me. I think "MyForm" is related to a trainer. Once I removed "MyForm" from the code it worked for me. I have been trying to figure out how to write to specific lines of a document but i'm not having any luck. for example writing to lines 3, 6, 9 and 12.
Code: | function saveSettings()
local settings = createStringlist()
settings.add( Top[3] )
settings.add( Left [6])
settings.add( Width[9] )
settings.add( Height[12] )
settings.saveToFile(filename)
settings.destroy()
end |
Also running this code overwrites what was already in the document. What if I want to keep some things in the document. Keep everything on lines 1 through to 10 but change something on line 11 through to 15. How would I adjust this code to do that ? |
|
Back to top |
|
|
Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
Posted: Wed Jul 31, 2019 11:58 pm Post subject: |
|
|
Use '\n' for the new line,
I am using this:
Code: | function saveSettings(MyForm)
path = TrainerOrigin or getMainForm()
local settingfile = io.open(path.."\\mysetting.ini", "r")
if not settingfile then return nil end
settingfile.close()
local settingfile = io.open(path.."\\mysetting.ini", "a+")
local top = MyForm.Top
local left = MyForm.Left
local width = MyForm.Width
local height = MyForm.Height
settingfile:write(top,"\n")
settingfile:write(left,"\n")
settingfile:write(width,"\n")
settingfile:write(height,"\n")
settingfile.close()
end
-- use:
saveSettings(UDF1) -- or form name |
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL |
|
Back to top |
|
|
|