[Gambas-user] Is it possible to read (and write) specific parts of a text file?

Rob Kudla sourceforge-raindog2 at ...94...
Tue Aug 15 18:48:00 CEST 2006


On Tue August 15 2006 12:02, LinuxSeeker wrote:
> I want to use a single configuration file (plain text) for my
> application. The format of the configuration file will be
> like:
>
> ----------------------------------
> #Program configuration
> [information]
> name: appname

This is pretty close to the format you'll get if you use the 
gb.settings component to manage your config files.  The only 
difference is that gb.settings uses "key=value" rather than 
"key: value".

http://gambasdoc.org/help/comp/gb.settings/

It's a lot easier than reinventing the wheel.  But if you wanted 
to make a change to a config value in a file like you describe, 
and didn't want to use the Settings class for some reason, I'd 
do something like this: (pseudocode because I don't have Gambas 
on this machine)

dim s as string
dim r as regexp ' need the gb.pcre component in your project

s = File.Load("config.ini") ' or whatever
r = new Regexp(s, "textbox1_text: .+\n")
s = Replace(s, r.Text, "textbox1_text: " & Textbox1.Text)
File.Save("config.ini", s)

As an aside, please use your real name on the Gambas lists if 
it's at all possible.

Thanks
Rob




More information about the User mailing list