Configuration file use

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

Configuration file use

#1 Post by 8-bit »

When one writes a program that gets configuration from a file, and variables or text are called from the configuration file with the program by a . configfile at the start of the program, all values and text become part of the program and the configfile becomes a blank slate. So one needs to write the values and text back to the configfile upon exiting said program.
As an example, in GTK tips and tricks, there is a program under Benefits of a configuration file in section 5.
If you run it, and check the contents of the /root/.testrc file before exiting, you will find it is blank even though it was created if it did not exist at program start.
When you exit the program though, the .testrc file is written again.
If you removed the part of the last line "> /root/.testrc", you would end up with an empty configuration file.

Has anyone else noticed this?

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#2 Post by 01micko »

I don't think it becomes a blank slate, I just opened a program that uses one and while running the config file was still there .. pristine.

I have used several different methods to alter the config files though, the one in ziggy's is ok for something very simple... stdout is just overwriting whatever was there before. I have used sed if the config needs altering, actually do this from the gtkdialog formed by a loop in Startmount. Don't ask me how I figured that one out! My head was sore afterward :lol:

Cheers
Puppy Linux Blog - contact me for access

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#3 Post by 8-bit »

I have and ran the program that is in GTK- tips and tricks in the programming section of the forum under section 5 of page 1.
I ran the program and it created a configuration file. Then, before I exited the program, I tried to open the same configuration file in a text editor.
The file showed with no contents.
But when I exited the program, the settings were written back to the file.
Try it!
Also, how would one read in a configuration file containing variables and text to be used by the program without using ". configfile"?
Wait a minute!
If a program accesses a configuration file in the above way, is the file open while the program is running and closed when the program exits and writes its settings?
Maybe that is why I am coming up with a blank file shown.
I am always trying to learn more.
I will stop learning when I die.
Then again, you never know!

DMcCunney
Posts: 889
Joined: Tue 03 Feb 2009, 00:45

#4 Post by DMcCunney »

8-bit wrote:I have and ran the program that is in GTK- tips and tricks in the programming section of the forum under section 5 of page 1.
I ran the program and it created a configuration file. Then, before I exited the program, I tried to open the same configuration file in a text editor.
The file showed with no contents.
But when I exited the program, the settings were written back to the file.
Try it!
It will create a blank config file when you run it. It won't actually put anything in the config file till you exit it. Part of what you are doing the first time you run the program is making the settings to be written to the config file. Until you do that, there's nothing to put in it.

What happens if you run the example program again after you have closed it and written the config file, and then open the config file in an editor while the program is running?
Also, how would one read in a configuration file containing variables and text to be used by the program without using ". configfile"?
Wait a minute!
If a program accesses a configuration file in the above way, is the file open while the program is running and closed when the program exits and writes its settings?
Maybe that is why I am coming up with a blank file shown.
More likely, because there's nothing in it to see until you create settings to put there.

The program should be able to open the config file, read in the settings, and then close it. There's probably no reason to keep it open while the program is running. If you make config changes within the program while it's running, it can always open it, write your changes, and close it again.

But no program I know that uses a config file opens it, reads in the settings and takes them out of the config file in the process, leaving it blank, then writes them all back when you close the program. Whatever for? Once created, the config file is usually static, and only needs to be opened and read when the program is run so it knows how to set itself up.

Some programs will use a config file if one exists, but will use built-in defaults if it doesn't - the config file is only there to let you change the default behavior.
______
Dennis

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#5 Post by sunburnt »

No matter what kind of code the executable file has in it, it should never wipe itself out!
In a file I just put:

Code: Select all

#!/bin/sh
echo '####'
A=AAA
I ran the file as:
# . z0-test
# echo `z0-test`
# AA= `z0-test`
# echo $A
AAA
All of the executions of the file worked as expected, and it never wiped itself out!

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#6 Post by 8-bit »

When I run the Benefits of a configuration file, I am able to :
1st it makes /root/.testrc if it does not exist.
It writes the first configuration file.
If no changes are requested, it exits and writes to root/.testrc
At this point the file, root/.testrc is populated (has contents).

So I run the program again.
While the program is running and no changes are yet made, I open the /root/.testrc file in an editor.
It has NO contents shown in the text editor.
I exit the program and check it again.
The root/.testrc file now has contents.

As I said before. Cut and paste the file from GTK- tips and tricks into an editor, save it, make it executable, and run the darn thing.

Maybe you can shed some light on it.

And for sunburnt, it is not the file you use to modify the configuration file that goes blank, it is the configuration file itself.

Again, try it.

I have tried this in Puppy 431, 432, lupu-5, and 421.

DMcCunney
Posts: 889
Joined: Tue 03 Feb 2009, 00:45

#7 Post by DMcCunney »

8-bit wrote:As I said before. Cut and paste the file from GTK- tips and tricks into an editor, save it, make it executable, and run the darn thing.

Maybe you can shed some light on it.
What file? There are 24 pages of posts in that thread. Which one do you mean?
______
Dennis

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#8 Post by sunburnt »

The file I ran is the config. file you speak of, there is no other file I spoke of...
The file I showed has a command: "echo ###" and a variable assignment: "A=AAA" ( config.).
Running or reading a file should never wipe it out! Sourced ". (file)" or just normally "(file)".

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#9 Post by 8-bit »

The file I am talking about uses a seperate file to save configuration and can be found here.
Look under section 5 of that first post for the program.
If you still cannot find the file, I am attaching it.
At first run, if its configuration file /root/.testrc does not exist, it will be created.
After exiting benconf by the Ok button, you can check that configuration file.
So, now you know it is populated.
Now run benconf again and while it is running, check the contents of the /root/.testrc file.

I hope that was clear enough.
Attachments
benconf.tar.gz
Extract to /root and run it. And be sure to try making a few changes as to checkboxes, dropdown seleection, and default text.
(687 Bytes) Downloaded 491 times

DMcCunney
Posts: 889
Joined: Tue 03 Feb 2009, 00:45

#10 Post by DMcCunney »

8-bit wrote:The file I am talking about uses a seperate file to save configuration and can be found here.
Look under section 5 of that first post for the program.
Check the first section of what you have carefully.

Line 3 should create a .testrc file with the following content:

Code: Select all

COMBOBOX="item 3"
ENTRY="default text"
RADIOBUTTON1="false"
RADIOBUTTON2="true"
Line 3 should look like this:

Code: Select all

[ ! -s $HOME/.testrc ] && echo -e -n 'COMBOBOX="item 3"\nENTRY="default text"\nRADIOBUTTON1="false"\nRADIOBUTTON2="true"\n' > $HOME/.testrc
and be all on one line

I have a sneaking suspicion that you have a line break in there, and the

Code: Select all

> $HOME/.testrc
segment is on a line by itself. That will cause it to be executed as a separate command, and create an empty .testrc file, without putting the default values into it. (That would produce the behavior you see.)

( "> foo" is a handy way to create a blank file called foo, or truncate it to zero length if it exists.)
______
Dennis

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#11 Post by 8-bit »

Look two posts up in this thread. I was a little late getting the file attached.
Also, the forum for some reason kicked part of the line that writes the first configuration file to the next line.
It was supposed to be all one line.
Try it again with the one I attached.

Post Reply