[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

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

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
prompt
Asked:
prompt
1 Solution
 
sunnycoderCommented:
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
 
zebadaCommented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jonathan6587Commented:
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
 
rstaveleyCommented:
In UN*X, you should look at shared memory IPC for this.
0
 
rstaveleyCommented:
...but a file is likely to suffice for most purposes.
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
grg99Commented:
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
 
rstaveleyCommented:
> You could write it back into the program binary

...and incur the wrath of your virus-checker :-)
0
 
promptAuthor Commented:
You all were very kind!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now