3

I want to store a letter into a variable so that whenever the program starts again it can use the variable in further program execution.

What is the way to achieve this?

laurisvr
  • 2,724
  • 6
  • 25
  • 44
UKNOWN
  • 63
  • 1
  • 1
  • 6
  • 2
    If it would have been possible then there should be no concept of file and folders – Subinoy May 20 '15 at 09:21
  • @Subinoy and _each_ program __for must__ should have unique variable names. :-) – Sourav Ghosh May 20 '15 at 09:37
  • 1
    definitively overkill and not what you're looking for, but it would be possible to compile a dll/so ad hoc and LoadLibrary it so that it contains a func or similar which return the value. But seriously, the answer depend on your concrete needs and may or may not involve direct file access. – wonko realtime May 20 '15 at 09:40
  • @wonkorealtime: On Windows that would be problematic as the DLL is read-only while running. Not impossible, but it makes the problem even harder. – MSalters May 20 '15 at 13:16
  • http://stackoverflow.com/questions/5316152/store-data-in-executable – udit043 Jun 04 '16 at 12:31
  • Even a shared memory segment would work. That way you don't even need a file. – Jerry Jeremiah Sep 10 '20 at 02:06

1 Answers1

16

No, you cannot achieve this using variables. variables only exist untill the program finishes execution. Once the program is finished, no variable is retained.

You need to make use of file i/o. You can write the end (final) data of one execution to a file, the next time program runs, it can read the file contains and resume the exection.

For your reference,

In C

you can check below library functions

and their families.

In C++

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261