Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > storing a list of struct in a file

Reply
Thread Tools

storing a list of struct in a file

 
 
major
Guest
Posts: n/a
 
      12-17-2007
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?
 
Reply With Quote
 
 
 
 
Rahul
Guest
Posts: n/a
 
      12-17-2007
On Dec 17, 2:59 pm, major <(E-Mail Removed)> wrote:
> 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...
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      12-17-2007
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-lit...alization.html

Michael
 
Reply With Quote
 
Lance Diduck
Guest
Posts: n/a
 
      12-17-2007
On Dec 17, 4:59 am, major <(E-Mail Removed)> wrote:
> 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?


On Dec 17, 4:59 am, major <(E-Mail Removed)> wrote:
> 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?


On Dec 17, 4:59 am, major <(E-Mail Removed)> wrote:
> 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/wg...2007/n2486.pdf This
container also has the property that it can be read and written from a
file.
Lance
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      12-17-2007
Michael DOUBEZ wrote:
> 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-lit...alization.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.


--
Jim Langston
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-18-2007
On Dec 17, 10:59 am, major <(E-Mail Removed)> wrote:

> 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.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
major
Guest
Posts: n/a
 
      12-20-2007
On Dec 17, 8:59 pm, major <(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Memory issues when storing as List of Strings vs List of List OW Ghim Siong Python 2 11-30-2010 12:22 PM
User Images: Storing in Files VS Storing in Database Jonathan Wood ASP .Net 1 06-02-2008 05:56 PM
storing pointer vs storing object toton C++ 11 10-13-2006 11:08 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments