storing a list of struct in a file

M

major

Hi all,
I would like to store (and read) a list of structs into a file using c+
+ (each struct contains integers and strings), and I'm wondering if it
there is a method to do it at "high level", I mean: I would like to
don't care of how the strucs are saved on the file, I want to just
save/load my list, is it possible using the std?
 
R

Rahul

Hi all,
I would like to store (and read) a list of structs into a file using c+
+ (each struct contains integers and strings), and I'm wondering if it
there is a method to do it at "high level", I mean: I would like to
don't care of how the strucs are saved on the file, I want to just
save/load my list, is it possible using the std?

What is the struct's members? does it contain any pointer variable?
Anyway, you could use ASCII file to store each member of the structure
in an ordered way or as a binary file and storing the entire contents
of the structure, make sure it doesn't have any pointer member
varibles...
 
M

Michael DOUBEZ

major a écrit :
Hi all,
I would like to store (and read) a list of structs into a file using c+
+ (each struct contains integers and strings), and I'm wondering if it
there is a method to do it at "high level", I mean: I would like to
don't care of how the strucs are saved on the file, I want to just
save/load my list, is it possible using the std?

There is no such thing in standard but there are many libraries out
there to do it.

See FAQ about serialization:
http://www.parashift.com/c++-faq-lite/serialization.html

Michael
 
L

Lance Diduck

Hi all,
I would like to store (and read) a list of structs into a file using c+
+ (each struct contains integers and strings), and I'm wondering if it
there is a method to do it at "high level", I mean: I would like to
don't care of how the strucs are saved on the file, I want to just
save/load my list, is it possible using the std?

Hi all,
I would like to store (and read) a list of structs into a file using c+
+ (each struct contains integers and strings), and I'm wondering if it
there is a method to do it at "high level", I mean: I would like to
don't care of how the strucs are saved on the file, I want to just
save/load my list, is it possible using the std?

Hi all,
I would like to store (and read) a list of structs into a file using c+
+ (each struct contains integers and strings), and I'm wondering if it
there is a method to do it at "high level", I mean: I would like to
don't care of how the strucs are saved on the file, I want to just
save/load my list, is it possible using the std?

It is possible with some implementations of the STL. The problem is
determining if your implementation can do it. It requires that the STL
containers actually use the pointer definition found in the allocator
parameter. So there is no portable way to do this, because the
standard doesnt require that an implementation actually use that
pointer definition. But a few do.
See the "Shared Memory container" example (pg 30) in this paper.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2486.pdf This
container also has the property that it can be read and written from a
file.
Lance
 
J

Jim Langston

Michael said:
major a écrit :

There is no such thing in standard but there are many libraries out
there to do it.

See FAQ about serialization:
http://www.parashift.com/c++-faq-lite/serialization.html

Michael

That is one thing I'd like C++ to have a type of for each for each variable
member of a class/structure. There are a few cases it would come in handy,
this being one of them. Another use would be tying a structure/class to a
database where I had to code each variable in the class.

You know, the constructor and detructor seem to have to be able to do this,
but I think that's on the compiler level, not C++ level.
 
J

James Kanze

I would like to store (and read) a list of structs into a file
using c+ + (each struct contains integers and strings), and
I'm wondering if it there is a method to do it at "high
level", I mean: I would like to don't care of how the strucs
are saved on the file, I want to just save/load my list, is it
possible using the std?

There's nothing standard. C++ doesn't define the representation
of anything, internally or externally. Internally, it typically
doesn't matter, but externally, you have to do it yourself (or
adopt some existing representation, like XDR). You can't even
really write an int, and expect to reread it reliably, without
defining its (or the file's) format. (Even in text a text, you
have to define things like separators.)
 
M

major

I would like to store (and read) a list of structs into a file using c+ [..]
save/load my list, is it possible using the std?

hi everybody,
thanks for your answer, I realised that saving a struct at "high
level" is not trivial, so I figured out a solution to do it in a
"lower level", not really elegant (or at least not as elegant as I
wanted) but it's fine.

cheers,
Mauro
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top