Saving Objects

Discussion in 'C++' started by JoeC, Jan 31, 2007.

  1. JoeC

    JoeC Guest

    I am writing a game and all my game pieces are stored in a single
    vector of piece handles. I have the basics I can read and write char
    and number files but I am trying to do comthing more complicated. I
    am trying to save the data I need to save and I can fill in some when
    the necessary data is saved.

    Vector->piece handle -> pointer to the pieces. How do I go about
    saving that data into a file. I have been doing some research to some
    sites: http://www.angelfire.com/country/aldev0/cpphowto/
    cpp_BinaryFileIO.html

    I seem to get somthing to save. So far in my handle for the pieces I
    have a save function that will have the pieces save themselvs. Here
    is the data I want to save:

    class unit{
    protected:

    coord loc;
    coord currentLoc;
    graphics * gr;
    tbox * combatBox;
    std::vector<color>colors;

    float attack;
    float dattack;
    float defence;

    More numbers...

    Here is how I am starting:
    void unit::write(std::fstream& f){

    f.write((char*)&loc, sizeof (loc));
    f.write((char*)&colors, sizeof (colors));

    Am I going about it the right or wrong way?
     
    JoeC, Jan 31, 2007
    #1
    1. Advertising

  2. On Jan 31, 11:56 am, "JoeC" <> wrote:
    > I am writing a game and all my game pieces are stored in a single
    > vector of piece handles. I have the basics I can read and write char
    > and number files but I am trying to do comthing more complicated. I
    > am trying to save the data I need to save and I can fill in some when
    > the necessary data is saved.
    >
    > Vector->piece handle -> pointer to the pieces. How do I go about
    > saving that data into a file. I have been doing some research to some
    > sites: http://www.angelfire.com/country/aldev0/cpphowto/
    > cpp_BinaryFileIO.html


    [snip]

    > Am I going about it the right or wrong way?


    The FAQ covers this subject in depth:

    http://www.parashift.com/c -faq-lite/serialization.html

    Best regards,

    Tom
     
    Thomas Tutone, Jan 31, 2007
    #2
    1. Advertising

  3. JoeC

    JoeC Guest

    On Jan 31, 11:21 am, "Thomas Tutone" <> wrote:
    > On Jan 31, 11:56 am, "JoeC" <> wrote:
    >
    > > I am writing a game and all my game pieces are stored in a single
    > > vector of piece handles. I have the basics I can read and write char
    > > and number files but I am trying to do comthing more complicated. I
    > > am trying to save the data I need to save and I can fill in some when
    > > the necessary data is saved.

    >
    > > Vector->piece handle -> pointer to the pieces. How do I go about
    > > saving that data into a file. I have been doing some research to some
    > > sites: http://www.angelfire.com/country/aldev0/cpphowto/
    > > cpp_BinaryFileIO.html

    >
    > [snip]
    >
    > > Am I going about it the right or wrong way?

    >
    > The FAQ covers this subject in depth:
    >
    > http://www.parashift.com/c -faq-lite/serialization.html
    >
    > Best regards,
    >
    > Tom


    Thanks I did go to that link in my research but I am not sure how it
    is supposed to help me. I don't see any exaples of saving data
    there. I created a two person game and I would like to create a play
    by e-mail system where after each turn the file of pieces are saved
    and sent off to play the next turn. I also have a city control object
    and the number of points each side has.

    Would it be better if I created a text file put in all the necessary
    information and sent that off and use the objects contsrtuctors and
    set the approate variables when the program is loaded back up. I have
    used files to some extent already, it is that I have yet to do
    anything complicated and I would like to get suggestions on stratigies
    or at least a starting point.
     
    JoeC, Jan 31, 2007
    #3
  4. JoeC

    Ondra Holub Guest

    JoeC napsal:
    > I am writing a game and all my game pieces are stored in a single
    > vector of piece handles. I have the basics I can read and write char
    > and number files but I am trying to do comthing more complicated. I
    > am trying to save the data I need to save and I can fill in some when
    > the necessary data is saved.
    >
    > Vector->piece handle -> pointer to the pieces. How do I go about
    > saving that data into a file. I have been doing some research to some
    > sites: http://www.angelfire.com/country/aldev0/cpphowto/
    > cpp_BinaryFileIO.html
    >
    > I seem to get somthing to save. So far in my handle for the pieces I
    > have a save function that will have the pieces save themselvs. Here
    > is the data I want to save:
    >
    > class unit{
    > protected:
    >
    > coord loc;
    > coord currentLoc;
    > graphics * gr;
    > tbox * combatBox;
    > std::vector<color>colors;
    >
    > float attack;
    > float dattack;
    > float defence;
    >
    > More numbers...
    >
    > Here is how I am starting:
    > void unit::write(std::fstream& f){
    >
    > f.write((char*)&loc, sizeof (loc));
    > f.write((char*)&colors, sizeof (colors));
    >
    > Am I going about it the right or wrong way?


    Hi. I recommend you to find some XML library and store your data in
    XML file. If XML is not for any reason what you can use, use text
    file. It will save you lot of time with reading data and it is better
    portable too.
     
    Ondra Holub, Jan 31, 2007
    #4
  5. JoeC

    Guest

    XML is a very good choice, simply because the format is standardized
    and it's human-readable. You should look into XML formats and decide
    if that's the course for you. If so, then try http://www.grinninglizard.com/tinyxml/,
    which I found to be a very easy-to-use C++ XML library. I might have
    some XML code around on my harddrive so if I'll poke around to see if
    there's anything that could be of use to you (I used to write games
    using TinyXML for saving/loading files as well).

    If you don't want to use XML, then the way you're going about it is
    fine as well, if your game is simple enough. Beware though, if you
    make changes between your saving routine and your loading routine
    you'll run into problems. For example, if you change the order in
    which you save but leave the order in which you load different
    variables, that'll run you up the crapper. XML will solve that problem
    because every variable is tagged, so you know *exactly* what you're
    loading.

    Hope that helps,
    Cheers,
    Henry

    http://hamath.blogspot.com - Science, Technology, Interesting Stuff
    Blog
     
    , Feb 1, 2007
    #5
  6. JoeC

    JoeC Guest

    On Jan 31, 6:34 pm, "" <>
    wrote:
    > XML is a very good choice, simply because the format is standardized
    > and it's human-readable. You should look into XML formats and decide
    > if that's the course for you. If so, then tryhttp://www.grinninglizard.com/tinyxml/,
    > which I found to be a very easy-to-use C++ XML library. I might have
    > some XML code around on my harddrive so if I'll poke around to see if
    > there's anything that could be of use to you (I used to write games
    > using TinyXML for saving/loading files as well).
    >
    > If you don't want to use XML, then the way you're going about it is
    > fine as well, if your game is simple enough. Beware though, if you
    > make changes between your saving routine and your loading routine
    > you'll run into problems. For example, if you change the order in
    > which you save but leave the order in which you load different
    > variables, that'll run you up the crapper. XML will solve that problem
    > because every variable is tagged, so you know *exactly* what you're
    > loading.
    >
    > Hope that helps,
    > Cheers,
    > Henry
    >
    > http://hamath.blogspot.com- Science, Technology, Interesting Stuff
    > Blog



    Thanks, I didn't know that XML was out there I though it was
    Extendable Marck-up Language.
     
    JoeC, Feb 1, 2007
    #6
  7. JoeC

    red floyd Guest

    JoeC wrote:
    > On Jan 31, 6:34 pm, "" <>
    > wrote:
    >> XML is a very good choice, simply because the format is standardized
    >> and it's human-readable. You should look into XML formats and decide

    >
    > Thanks, I didn't know that XML was out there I though it was
    > Extendable Marck-up Language.


    It is. XML stands for eXtensible Markup Language. However, gamedia is
    right. It's an excellent language for storing structured data.
     
    red floyd, Feb 1, 2007
    #7
  8. JoeC

    Guest

    It is, XML stands for eXtensible Markup Language. But the power of it
    comes from allowing you to custom-define any sort of data you want,
    which is helpful. Anyways, good luck!

    Cheers, Henry Yuen
    --------
    http://hamath.blogspot.com - Science, technology, and interesting
    stuff Blog
     
    , Feb 1, 2007
    #8
  9. JoeC

    red floyd Guest

    wrote:
    > It is, XML stands for eXtensible Markup Language. But the power of it
    > comes from allowing you to custom-define any sort of data you want,
    > which is helpful. Anyways, good luck!
    >


    Darn near identical and simultaneous posts! Wow!
     
    red floyd, Feb 1, 2007
    #9
  10. JoeC

    Guest

    On Jan 31, 9:32 pm, red floyd <> wrote:
    > wrote:
    > > It is, XML stands for eXtensible Markup Language. But the power of it
    > > comes from allowing you to custom-define any sort of data you want,
    > > which is helpful. Anyways, good luck!

    >
    > Darn near identical and simultaneous posts! Wow!


    That is jaw-dropping.
     
    , Feb 1, 2007
    #10
  11. JoeC

    JoeC Guest

    On Jan 31, 11:32 pm, red floyd <> wrote:
    > wrote:
    > > It is, XML stands for eXtensible Markup Language. But the power of it
    > > comes from allowing you to custom-define any sort of data you want,
    > > which is helpful. Anyways, good luck!

    >
    > Darn near identical and simultaneous posts! Wow!




    I don't know much about XML but trying the save information with
    fstream is getting pretty cumbersome. It dosn't even work when I am
    trying to save information in a vector. When I get some time I will
    take a closer look at XML
     
    JoeC, Feb 1, 2007
    #11
    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. John Kandell
    Replies:
    4
    Views:
    4,228
    eeebop
    Dec 10, 2004
  2. Luis Esteban Valencia
    Replies:
    0
    Views:
    2,555
    Luis Esteban Valencia
    Jan 6, 2005
  3. Eagle

    Saving objects to disk

    Eagle, Feb 5, 2004, in forum: C++
    Replies:
    4
    Views:
    417
    Chris Theis
    Feb 6, 2004
  4. 7stud
    Replies:
    11
    Views:
    750
    Dennis Lee Bieber
    Mar 20, 2007
  5. Lovely Angel For You

    Saving Images While Saving ASP Pages !

    Lovely Angel For You, Oct 2, 2003, in forum: ASP General
    Replies:
    1
    Views:
    230
    Curt_C [MVP]
    Oct 3, 2003
Loading...

Share This Page