How to run a program that retains a value even after execution is stopped?

JohnCTX 636 Reputation points
2021-08-06T01:30:20.107+00:00

I may have noticed that for some programming languages, users can re-execute a program and restores its variables with its assigned values.

Let's say first execution output:

1

User exits the program.

Then user re-executes the stored variable with its assigned value, but wants to update its value by using an increment.

The second execution output:

2

User exits the program again.

Can anyone provide me some examples?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,915 questions
{count} votes

Accepted answer
  1. Sam of Simple Samples 5,541 Reputation points
    2021-08-06T02:21:49.99+00:00

    For MFC you can use the CWinApp Class, more specifically CWinApp::WriteProfileString, CWinApp::WriteProfileInt, CWinApp::GetProfileString, CWinApp::GetProfileInt and probably CWinApp::SetRegistryKey. For Win32 API programs there are WriteProfileInt, WriteProfileString, GetProfileInt and GetProfileString functions that only work for INI files. For Win32 API programs there are also registry functions.

    You did not specify what type of program you are writing.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. WayneAKing 4,926 Reputation points
    2021-08-06T05:14:57.067+00:00

    Simple data persistence may be implemented using techniques such
    as those suggested by others. That differs little from regular
    data storage and retrieval. However, more sophisticated and
    complex requirements such as saving and restoring the state
    of a class object is usually more difficult and complicated.

    [39.10] What is "persistence"? What is a "persistent object"?
    https://www.cs.technion.ac.il/users/yechiel/c++-faq/persistence.html

    "A persistent object can live after the program which created it has
    stopped. Persistent objects can even outlive different versions of the
    creating program, can outlive the disk system, the operating system,
    or even the hardware on which the OS was running when they were created."

    STLplus C++ Library Collection
    Persistence Library
    http://stlplus.sourceforge.net/stlplus3/docs/persistence.html

    "Persistence is the ability to dump a data structure to a "serialised"
    form and then restore it later either in the same run of the program,
    a later run of a program, or even in a different program. It is an easy
    way to save a program's state or to communicate information in a
    structural form between programs."

    Serialization and Unserialization
    https://isocpp.org/wiki/faq/serialization

    "What's this 'serialization' thing all about?"

    "It lets you take an object or group of objects, put them on a disk
    or send them through a wire or wireless transport mechanism, then
    later, perhaps on another computer, reverse the process: resurrect
    the original object(s). The basic mechanisms are to flatten object(s)
    into a one-dimensional stream of bits, and to turn that stream of
    bits back into the original object(s)."

    Exactly how to incorporate persistence/serialization into your
    own program(s) depends on what language you are using, what
    frameworks or APIs/SDKs that are available and being used by
    you, etc. For example, here is how .NET addresses the topic:

    Serialization in .NET
    https://learn.microsoft.com/en-us/dotnet/standard/serialization/

    "Serialization is the process of converting the state of an
    object into a form that can be persisted or transported. The
    complement of serialization is deserialization, which converts
    a stream into an object. Together, these processes allow data
    to be stored and transferred."

    • Wayne
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.