J
jacob navia
Hi
What would be the best way to save and reload later a container
to/from disk in C++?
Thanks
What would be the best way to save and reload later a container
to/from disk in C++?
Thanks
Hi
What would be the best way to save and reload later a container
to/from disk in C++?
It depends, there isn't really a best way. Do you want portability?
http://www.boost.org/doc/libs/1_42_0/libs/serialization/doc/index.html
Is a good place to start.
Hi
What would be the best way to save and reload later a container
to/from disk in C++?
Thanks
I'm not sure how best to deal with binary data, but for
numbers and text, I would use JSON (escaping special
characters as appropriate, etc).
It's well-understood, lightweight and simple, and
portable across many languages.
With binary data you could base64-encode it or something,
but you'll be looking at significant bloat for large
structures. Or you could NUL-separate fields, replacing
actual NUL characters with \001s, and actual \001s with
\001\001s.
Andrew Poelstra a écrit :
Well, but that is a significant development. I thought that the
STL would provide something to save/restore a container.
jacob said:Andrew Poelstra a écrit :
Well, but that is a significant development. I thought that the
STL would provide something to save/restore a container.
That would be a little tricky because everything is templated. Now, suppose
you want to save/restore a vector<T>. The most natural thing would be to use
operator<< and operator>> for serializing and deserializing vector elements.
That, however, would suppose that for elements of type T both operations are
truly inverse. This does not even hold for the standard types (e.g., double
or std::string).
Then, for set<T,C>, it is not clear what to do about the comparison
predicate. Also: would you save/restore allocator objects or would you
decide to ignore the issue?
It depends, there isn't really a best way. Do you want portability?
http://www.boost.org/doc/libs/1_42_0/libs/serialization/doc/index.html
That would be a little tricky because everything is templated. Now, suppose
you want to save/restore a vector<T>. The most natural thing would be to use
operator<< and operator>> for serializing and deserializing vector elements.
That, however, would suppose that for elements of type T both operations are
truly inverse. This does not even hold for the standard types (e.g., double
or std::string).
Then, for set<T,C>, it is not clear what to do about the comparison
predicate.
Also: would you save/restore allocator objects or would you
decide to ignore the issue?
Kai-Uwe Bux a écrit :
Excuse me but I do not understand that. If I write a double value into a
file, I will obtain the same value when I read it later if I store it in
binary form.
fwrite(&Double,1,sizeof(double),stream);
fread(&Double,1,sizeof(double),stream);
will leave the value of Double unchanged. Obviously if you use the same
CPU type for bothoperations.
The container could be read in an incomplete way so that most values are
retrieved but function pointers aren't.
Kai-Uwe Bux a crit :
Excuse me but I do not understand that. If I write a double value into a
file, I will obtain the same value when I read it later if I store it in
binary form.
fwrite(&Double,1,sizeof(double),stream);
fread(&Double,1,sizeof(double),stream);
will leave the value of Double unchanged. Obviously if you use the same
CPU type for bothoperations.
I just do not see how that could be wrong. Maybe you care to explain?
[...]jacob said:Kai-Uwe Bux a écrit :
Excuse me but I do not understand that. If I write a double value into a
file, I will obtain the same value when I read it later if I store it in
binary form.
fwrite(&Double,1,sizeof(double),stream);
fread(&Double,1,sizeof(double),stream);
will leave the value of Double unchanged. Obviously if you use the same
CPU type for bothoperations.
I just do not see how that could be wrong. Maybe you care to explain?
The container could be read in an incomplete way so that most values are
retrieved but function pointers aren't.
See above.
Well, the C++ compiler for zOS can be run (with the equivalent of a
command line switch) to use either hex or binary (IEEE) float. Most
definitely not the same format, even if doubles are 64 bits long in
both cases. The saga of long doubles on Windows (and other x86
platforms) is another example, where some compilers treat long double
as a 64 bit IEEE double, and other use the 80 bit double extended
format.
(e-mail address removed) a écrit :
(1) Obviously if you are working with zOS you rather use the same
command line switch for reading and writing your double data.
Why is that so difficult to understand?
(2) Obviously too, double data is not portable among compilers that
use different representations of the data. If one compiler
implements long double as 64 bits and the other as 80 bits
they aren't compatible and you should recompile both the reader
and the writer.
Why is that so difficult to understand?
Here we arrive at philosophical questions. C++ is all about bells
and whistles. Rather than providing a solution that would be very useful
for most users but would fail in some special cases it is decided that
nothing should be provided so that everyone rolls its own.
(e-mail address removed) a écrit :
(1) Obviously if you are working with zOS you rather use the same
command line switch for reading and writing your double data.
Why is that so difficult to understand?
(2) Obviously too, double data is not portable among compilers that
use different representations of the data. If one compiler
implements long double as 64 bits and the other as 80 bits
they aren't compatible and you should recompile both the reader
and the writer.
Why is that so difficult to understand?
Here we arrive at philosophical questions. C++ is all about bells
and whistles. Rather than providing a solution that would be very useful
for most users but would fail in some special cases it is decided that
nothing should be provided so that everyone rolls its own.
Modulo bugs the Boost solution seems to be working. Other solutions were
presented in this discussion (http://webEbenezer.netby Brian)
How do you enforce that?
What if you don't have the source for one or both of them?
It isn't difficult to understand, it's impractical. There isn't a
standard format for double.
Have you read the responses here? The problem of serialisation is
complex and multi-layered. Sure a naive solution would work for a
subset of types, but that subset is small. As soon as you add any
complexity to your objects (even something as trivial as float or
pointers), the solution breaks down. So it isn't "very useful for most
users".
It's not difficult to understand at all. The problem is that you
stated that it should not be a problem "if you use the same CPU type
for bothoperations. " Which is clearly incorrect. It's not even
consistent for a single compiler on a single OS on that one CPU type.
Hi
What would be the best way to save and reload later a container
to/from disk in C++?
Thanks
I'm not sure how best to deal with binary data, but for
numbers and text, I would use JSON (escaping special
characters as appropriate, etc).
It's well-understood, lightweight and simple, and
portable across many languages.
With binary data you could base64-encode it or something,
but you'll be looking at significant bloat for large
structures. Or you could NUL-separate fields, replacing
actual NUL characters with \001s, and actual \001s with
\001\001s.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.