Solved

Variable which gets a value when the program starts and the value is saved.

Posted on 2003-12-01
10
318 Views
Last Modified: 2010-04-15
Can i use and how (if i can) a variable, which is changed when the program runs and after the program is closed the same variable to has the new value. This can be easily made by writting the value into a text file and every time the program is started the file is read but i don't want use files. Thank you for your answer.
0
Comment
Question by:prompt
10 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
Hi prompt,

there is no way to maintain the value of a variable across different runs unless persistent storage is used and C provides persistent storage in the form of files ... So you are out of luck :o)

Cheers!
Sunny:o)
0
 
LVL 6

Expert Comment

by:zebada
Comment Utility
What platform?
Do you want the value to remain evan if them machine is powered off?

If you only need the value to be "saved" for only as long as the machine is running you could save the value(s) to shared memory on UNIX.
I'm sure a similar construct exists on Windows - or whatever operating system you are using.

If the value is to be saved even when the machine is powered down then you really have no alternative but to write it to a file, unless you have access to some other form of permanent store.

Regards
Paul
0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
Hi prompt,

As sunnycoder and zebada have said, you can't.  (Well, almost can't.)

Your environment variables are a tightly packed structure that doesn't have room for growth.  It is also "surrounded" by other data items that don't want to be moved.  So adding items to the established environment variables is tough.  Windows and *nix developers have also made it tough to change or delete environment variables.

The reasoning is pretty simple -- if you've gone to the trouble to build an environment that works for a given application, you certainly don't want programs to modify it for their own purposes.  Imagine the problems that could arise if a program were to change HOME, USER, or SHELL and not put them back when it was done!

When a program is run, the operating system does a lot of "setup" work to make sure that the program is given a clean environment.  One of the things that it does is copy the current environment variables to a block of memory accessable by the program (task).  The program is then free to query and even modify these variables for its own use (using getenv(), putenv(), env() or other functions).  However, the program is only affecting the copy that has been created for it.  Changes to the environment variables do NOT affect the parent task that started the program.

However, you can "simulate" changing environment variables.  From within your program, make your changes to your environment variables.  Now execute a shell.  The shell will inherit the environment that you established.


Good Luck,
Kent
0
 
LVL 1

Accepted Solution

by:
jonathan6587 earned 125 total points
Comment Utility
If you are using a windows machine, you could always write the value to the registry.  Then you could read the value from the registry the next time the program is started - even after a reboot.  But, really we need more information on what you want to do.

If this is what you want I can provide an example.

Good Luck,
Jonathan
0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
In UN*X, you should look at shared memory IPC for this.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
...but a file is likely to suffice for most purposes.
0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility

One more thing....

File access on *nix machines if very fast.  If you wind up using a file to keep your controls (and remember, there are literally millions of programs that get their configurations from .ini and .cfg files) you can often open and read the file with no physical disk I/O.  This is because the the page is often buffered by the operating system so that the next read gets the page back from memory.

Don't be afraid to use a file for your data.  That's what files are for!
Kent
0
 
LVL 22

Expert Comment

by:grg99
Comment Utility
You could write it back into the program binary.  Something like this:

#define ThePw "---Something unique---"
#define TheIntPw   1234

struct identifiable { char Pw1[] = ThePw;  int IntPw=TheIntPw; int var1, var2, etc....; char Pw2[]=ThePw; }

f = open( argv[0], "wb" );
read until you find ThePw,  followed by IntPw,
then write the changed values  for var1, var2, etc...



 
0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
> You could write it back into the program binary

...and incur the wrath of your virus-checker :-)
0
 

Author Comment

by:prompt
Comment Utility
You all were very kind!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
port mapped I/o query 10 136
Need example 5 100
Problem to save 10 120
How do I set up two external hard drives with Acronis True Image? 12 51
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now