storing a list of struct in a file

Discussion in 'C++' started by major, Dec 17, 2007.

  1. major

    major Guest

    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?
     
    major, Dec 17, 2007
    #1
    1. Advertising

  2. major

    Rahul Guest

    On Dec 17, 2:59 pm, major <> 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...
     
    Rahul, Dec 17, 2007
    #2
    1. Advertising

  3. 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
     
    Michael DOUBEZ, Dec 17, 2007
    #3
  4. major

    Lance Diduck Guest

    On Dec 17, 4:59 am, major <> 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 <> 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 <> 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/wg21/docs/papers/2007/n2486.pdf This
    container also has the property that it can be read and written from a
    file.
    Lance
     
    Lance Diduck, Dec 17, 2007
    #4
  5. major

    Jim Langston Guest

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


    --
    Jim Langston
     
    Jim Langston, Dec 17, 2007
    #5
  6. major

    James Kanze Guest

    On Dec 17, 10:59 am, major <> 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:
    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
     
    James Kanze, Dec 18, 2007
    #6
  7. major

    major Guest

    On Dec 17, 8:59 pm, major <> 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
     
    major, Dec 20, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA Scheltema
    Replies:
    3
    Views:
    404
    RA Scheltema
    Jan 6, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,390
    Chris Fogelklou
    Apr 20, 2004
  3. toton
    Replies:
    11
    Views:
    711
    toton
    Oct 13, 2006
  4. Jonathan Wood
    Replies:
    1
    Views:
    511
    Jonathan Wood
    Jun 2, 2008
  5. OW Ghim Siong
    Replies:
    2
    Views:
    393
    Peter Otten
    Nov 30, 2010
Loading...

Share This Page