• 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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Kent OlsenDBACommented:
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,
Turn Raw Data into a Real Career

There’s a growing demand for qualified analysts who can make sense of Big Data. With an MS in Data Analytics, you can become the data mining, management, mapping, and munging expert that today’s leading corporations desperately need.

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,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
In UN*X, you should look at shared memory IPC for this.
...but a file is likely to suffice for most purposes.
Kent OlsenDBACommented:

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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.