question

JohnCTX-6479 avatar image
0 Votes"
JohnCTX-6479 asked WayneAKing-0228 answered

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

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++
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The simplest way is to store the desired value(s) in a small file similar to how browsers use cookies.

Not so simple methods include storing the data in the registry or writing the data back into the exe file.

0 Votes 0 ·

Before browsers had cookies Windows had INI files.

0 Votes 0 ·
SimpleSamples avatar image
1 Vote"
SimpleSamples answered SimpleSamples commented

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.









· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I want to write a program that retains variables' values prior to re-execution of the program.

What does the CWinApp class apply to? Does it apply to console and graphical interfaces as well?

Should I also use those methods under the CWinApp class to make this simpler?

0 Votes 0 ·

The CWinApp class applies to all MFC programs.

0 Votes 0 ·
WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered

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://docs.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









5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.