Read/Write/Update text file problem

hi

Now I’ve basic knowledge for read/write file but I’ve question about
how to edit value in text file by using keyword?

If assume File.txt and have content as below

============ File.txt =============  
  
Item: 1  
Player\_Name : Jonh  
Player\_Power: 1000  
Level:5  
  
===================================  

if I need to update value by keyword Player_Power set to 2000.
How to coding for this scenario?
Someone can be guide or share code?

Thanks in Advance
Mate [import]uid: 58733 topic_id: 12884 reply_id: 312884[/import]

Personally I would use the built in json libraries and not mess with the text files at all.

[lua]local json = require “json”
local settingsFile = “mygamesettings.json”
local gameSettings = {}

local function loadSettings()
local path = system.pathForFile( settingsFile, system.DocumentsDirectory)
local contents = “”
local file = io.open( path, “r” )
if file then
– read all contents of file into a string
local contents = file:read( “*a” )
gameSettings = json.decode(contents);
io.close( file )
end
end

local function saveSettings()
local filePath = system.pathForFile( settingsFile, system.DocumentsDirectory)
local file = io.open(filePath, “w”)
if file then
local contents = json.encode(gameSettings)
file:write( contents )
io.close( file )
return true
else
return false
end

end[/lua]

So in this case, the table gameSettings will hold your values and all you have to do is say:

gameSettings.Player_Power = 2000

or

gameSettings.Player_Power = gameSettings.Player_Power + 1000

then call the saveSettings function

But basically if you want to use the text file, you have to use the string API to parse each line read from the file and build your own variables.
[import]uid: 19626 topic_id: 12884 reply_id: 47310[/import]

or you can use the PropertyBag class and add it to your project. It allows you to set up a series of such variables and does all the file handling, freeing you to focus on get ting or set ting the values.

you can get that from GitHub here

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12884 reply_id: 47327[/import]

Thanks robmiracle and Jayantv

I think 2 solution are the best way for file management. [import]uid: 58733 topic_id: 12884 reply_id: 47424[/import]

Hi jayantv

OK PropertyBag class is useful for me, But I can’t pass parameter to function init ( params ) for create a new file.

Example

Property:prop.init(newfile.txt)

Can you suggest me?

Thanks
Mate [import]uid: 58733 topic_id: 12884 reply_id: 47865[/import]

just made a few changes to the code at GitHub at https://github.com/JayantVarma/Rating-Dialog-CoronaSDK

if you pick up the property.lua file from there, you will find that there are two new functions added,

getFilePathFor which would not be used by you, but another one called setFilenameTo

so to save to a new file, you need to
[lua] Property:setFilenameTo(“newFile”)[/lua]
and now do all the things that you want to normally with it. If you want to load from an alternative file at start, you will need to also call loadFromFile() immediately after you setFilenameTo()
cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12884 reply_id: 47919[/import]

?Hi jayantv
Yesterday, I’ve to do as your suggestion but it not work.
if I’ve 2 text files, can you tell me how to use as your suggestion if I need to read value of level and power for calculate and need to change level = 10 and change power=200.

===== A.txt ======

level=1  
score = 100  

==== B.txt =======

power=100  
Player\_score = 1000  

Thanks in Advance.
Mate [import]uid: 58733 topic_id: 12884 reply_id: 48555[/import]

@Mate,

[lua] prop:setFilenameTo(“A.txt”) – First set the filename to A.txt
prop:GetFromFile() – Load the data from that file
– Always run that after changing the filename (just in case)

level = tonumber(prop:getProperty(“level”,1))
– The default is 1 the first time
score = tonumber(prop:getProperty(“score”,100))
– The default is 100 the first time
prop:SaveToFile() – Save the data to the file
prop:setFilenameTo(“B.txt”) – First set the filename to A.txt
prop:GetFromFile() – Load the data from that file
– Always run that after changing the filename (just in case)

prop:getProperty(“power”,100) – The default is 1 the first time
playerscore = tonumber(prop:getProperty(“player_score”,1000))
– The default is 100 the first time
prop:SaveToFile() – Save the data to the file[/lua]

I do not want to ask why you have two different files for the scores, etc when you can manage them with one.

Now somewhere in your code you update these values and work with them, so to save them back, you would use
[lua] prop:setFilenameTo(“A.txt”) – First set the filename to A.txt
prop:GetFromFile() – Load the data from that file
prop:setProperty(“level”,10)
prop:SaveToFile() – Save the data to the file

prop:setFilenameTo(“B.txt”) – First set the filename to A.txt
prop:GetFromFile() – Load the data from that file
prop:setProperty(“power”,200)
prop:SaveToFile() – Save the data to the file[/lua]

hope that resolves your issue. and if you say that it does not work, you will have to provide a bit more to help you out on this than it doesn’t work.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12884 reply_id: 48566[/import]

?Hi jayantv

RightNow, I can to did as your suggestion. :wink:
your code is good example.

Thank a lot
Mate [import]uid: 58733 topic_id: 12884 reply_id: 48744[/import]