Solved

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

Posted on 2003-12-01
10
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9849492
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
ID: 9849500
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:Kent Olsen
ID: 9850195
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
Independent Software Vendors: 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!

 
LVL 1

Accepted Solution

by:
jonathan6587 earned 125 total points
ID: 9850220
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
ID: 9851328
In UN*X, you should look at shared memory IPC for this.
0
 
LVL 17

Expert Comment

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

Expert Comment

by:Kent Olsen
ID: 9852000

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
ID: 9852020
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
ID: 9852056
> You could write it back into the program binary

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

Author Comment

by:prompt
ID: 9856319
You all were very kind!
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

717 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