By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

Weird Question

P: n/a
Hi,
I'm not verry good at c++ but I was asking myself this question... Could it
be possible to store a variable into a file and load it back into the same
variable? What I would like to do is save a structure into a file so I can
load it back the next time my program run into the same structure.

Thanks, Max.
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Maximus" <ik*********@videotron.ca> wrote...
I'm not verry good at c++ but I was asking myself this question... Could it be possible to store a variable into a file and load it back into the same
variable? What I would like to do is save a structure into a file so I can
load it back the next time my program run into the same structure.


It is entirely possible and, as a matter of fact, used by many applications
to keep track of user preferences, for example. Some kind of structure (or
an array thereof) is read from the settings file (if it exists) and at the
end of the run (before quitting) saved into the same file...

The simplest way to deal with incompatibilities is to save your structure
in text form, BTW.

Victor

Jul 19 '05 #2

P: n/a
On Tue, 11 Nov 2003 22:09:12 -0800, Maximus <ik*********@videotron.ca>
wrote:
Hi,
I'm not verry good at c++ but I was asking myself this question... Could
it
be possible to store a variable into a file and load it back into the
same
variable? What I would like to do is save a structure into a file so I
can
load it back the next time my program run into the same structure.

Thanks, Max.

Most complicated thing is to remember the name of the file, so let's call
it remember.dat
write some stuff to the file:

struct POD {... }; // something in there, no pointers !
POD data;

#include <fstream>
ofstream os("remember.dat"); // creates a file to remember
os.write((const char*)&data,sizeof(POD));
If you want to read this back
POD data;

#include <fstream>
ifstream is("remember.dat"); // opens the data
is.read((char*)&data,sizeof(POD));

--
grzegorz
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.