• C

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.
Who is Participating?
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,
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)

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.

WEBINAR: 10 Easy Ways to Lose a Password

Join us on June 27th at 8 am PDT to learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees. We'll cover the importance of multi-factor authentication and how these solutions can better protect your business!

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,
In UN*X, you should look at shared memory IPC for this.
...but a file is likely to suffice for most purposes.
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!
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...

> You could write it back into the program binary

...and incur the wrath of your virus-checker :-)
promptAuthor Commented:
You all were very kind!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.