serialize a structure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • markww

    #1

    serialize a structure

    Hi,

    I have a data structure that looks like this:

    struct MY_STRUCT {
    int x,y,z;
    string str1;
    string str2;
    vector<floatvfl oats1;
    vector<floatvfl oats2;
    }

    I'm trying to save this structure into a 3rd party file to be read back
    later. I was told I could serialize this structure into a stream of
    bytes and then dump it into the file.

    For this type of stuff I'm used to creating a delimited version of the
    struct, using pipes for example:


    x|y|z|str1.leng th|str1|str2.le ngth|str2|vfloa ts1.size()|vflo ats1|vfloats2.s ize()|vfloats|

    but I'm wondering if there is a simpler way of 'serializing' the
    structure into a stream of bytes? Any ideas would be great. I am also
    using MFC by the way, I see they have their own serialization scheme,
    that is also a possibility for me to take advantage of,

    Thanks

  • mlimber

    #2
    Re: serialize a structure

    markww wrote:
    Hi,
    >
    I have a data structure that looks like this:
    >
    struct MY_STRUCT {
    int x,y,z;
    string str1;
    string str2;
    vector<floatvfl oats1;
    vector<floatvfl oats2;
    }
    >
    I'm trying to save this structure into a 3rd party file to be read back
    later. I was told I could serialize this structure into a stream of
    bytes and then dump it into the file.
    >
    For this type of stuff I'm used to creating a delimited version of the
    struct, using pipes for example:
    >
    >
    x|y|z|str1.leng th|str1|str2.le ngth|str2|vfloa ts1.size()|vflo ats1|vfloats2.s ize()|vfloats|
    >
    but I'm wondering if there is a simpler way of 'serializing' the
    structure into a stream of bytes? Any ideas would be great. I am also
    using MFC by the way, I see they have their own serialization scheme,
    that is also a possibility for me to take advantage of,
    >
    Thanks
    See these FAQs:



    You might want to consider Boost.Serializa tion:



    As for MFC, you'll have to ask on a Microsoft newsgroup (see the list
    at http://www.parashift.com/c++-faq-lit....html#faq-5.9).

    Cheers! --M

    Comment

    • Gernot Frisch

      #3
      Re: serialize a structure


      "markww" <markww@gmail.c omschrieb im Newsbeitrag
      news:1156773897 .989394.155270@ i3g2000cwc.goog legroups.com...
      Hi,
      >
      I have a data structure that looks like this:
      >
      struct MY_STRUCT {
      int x,y,z;
      string str1;
      string str2;
      vector<floatvfl oats1;
      vector<floatvfl oats2;
      std::ostream& operator << (std::ostream& os)
      {
      os << x << y << z << str1 << str1 << str2;
      os << vfloats1.size() ;
      for (int i=0; i<vflaots1.size ; ++i) os << vfloats1[i];
      os << vfloats2.size() ;
      for (int i=0; i<vflaots2.size ; ++i) os << vfloats2[i];
      return os;
      }
      }

      You might get into trouble with the strings, though, since some
      delimiter characters as '\n' within a std::string can't be read in
      this way.



      Comment

      • markww

        #4
        Re: serialize a structure


        Gernot Frisch wrote:
        "markww" <markww@gmail.c omschrieb im Newsbeitrag
        news:1156773897 .989394.155270@ i3g2000cwc.goog legroups.com...
        Hi,

        I have a data structure that looks like this:

        struct MY_STRUCT {
        int x,y,z;
        string str1;
        string str2;
        vector<floatvfl oats1;
        vector<floatvfl oats2;
        >
        std::ostream& operator << (std::ostream& os)
        {
        os << x << y << z << str1 << str1 << str2;
        os << vfloats1.size() ;
        for (int i=0; i<vflaots1.size ; ++i) os << vfloats1[i];
        os << vfloats2.size() ;
        for (int i=0; i<vflaots2.size ; ++i) os << vfloats2[i];
        return os;
        }
        >
        }
        >
        >
        You might get into trouble with the strings, though, since some
        delimiter characters as '\n' within a std::string can't be read in
        this way.
        Gernot, how would I read that back in?

        Thanks

        Comment

        • mlimber

          #5
          Re: serialize a structure

          markww wrote:
          Gernot Frisch wrote:
          "markww" <markww@gmail.c omschrieb im Newsbeitrag
          news:1156773897 .989394.155270@ i3g2000cwc.goog legroups.com...
          Hi,
          >
          I have a data structure that looks like this:
          >
          struct MY_STRUCT {
          int x,y,z;
          string str1;
          string str2;
          vector<floatvfl oats1;
          vector<floatvfl oats2;
          std::ostream& operator << (std::ostream& os)
          {
          os << x << y << z << str1 << str1 << str2;
          os << vfloats1.size() ;
          for (int i=0; i<vflaots1.size ; ++i) os << vfloats1[i];
          os << vfloats2.size() ;
          for (int i=0; i<vflaots2.size ; ++i) os << vfloats2[i];
          return os;
          }
          }

          You might get into trouble with the strings, though, since some
          delimiter characters as '\n' within a std::string can't be read in
          this way.
          >
          Gernot, how would I read that back in?
          As the FAQs say
          (http://www.parashift.com/c++-faq-lit...html#faq-36.7),
          you would use a similarly constructed extraction operator for a
          std::istream.

          Cheers! --M

          Comment

          • mlimber

            #6
            Re: serialize a structure


            mlimber wrote:
            markww wrote:
            Gernot Frisch wrote:
            "markww" <markww@gmail.c omschrieb im Newsbeitrag
            news:1156773897 .989394.155270@ i3g2000cwc.goog legroups.com...
            Hi,

            I have a data structure that looks like this:

            struct MY_STRUCT {
            int x,y,z;
            string str1;
            string str2;
            vector<floatvfl oats1;
            vector<floatvfl oats2;
            >
            std::ostream& operator << (std::ostream& os)
            {
            os << x << y << z << str1 << str1 << str2;
            os << vfloats1.size() ;
            for (int i=0; i<vflaots1.size ; ++i) os << vfloats1[i];
            os << vfloats2.size() ;
            for (int i=0; i<vflaots2.size ; ++i) os << vfloats2[i];
            return os;
            }
            >
            }
            >
            >
            You might get into trouble with the strings, though, since some
            delimiter characters as '\n' within a std::string can't be read in
            this way.
            Gernot, how would I read that back in?
            >
            As the FAQs say
            (http://www.parashift.com/c++-faq-lit...html#faq-36.7),
            you would use a similarly constructed extraction operator for a
            std::istream.
            PS, you'll need to add some sort of delimeter between elements, whether
            a space, comma, new line, or whatever. Otherwise the unserializing
            won't be possible.

            Cheers! --M

            Comment

            • markww

              #7
              Re: serialize a structure


              mlimber wrote:
              mlimber wrote:
              markww wrote:
              Gernot Frisch wrote:
              "markww" <markww@gmail.c omschrieb im Newsbeitrag
              news:1156773897 .989394.155270@ i3g2000cwc.goog legroups.com...
              Hi,
              >
              I have a data structure that looks like this:
              >
              struct MY_STRUCT {
              int x,y,z;
              string str1;
              string str2;
              vector<floatvfl oats1;
              vector<floatvfl oats2;

              std::ostream& operator << (std::ostream& os)
              {
              os << x << y << z << str1 << str1 << str2;
              os << vfloats1.size() ;
              for (int i=0; i<vflaots1.size ; ++i) os << vfloats1[i];
              os << vfloats2.size() ;
              for (int i=0; i<vflaots2.size ; ++i) os << vfloats2[i];
              return os;
              }

              }


              You might get into trouble with the strings, though, since some
              delimiter characters as '\n' within a std::string can't be read in
              this way.
              >
              Gernot, how would I read that back in?
              As the FAQs say
              (http://www.parashift.com/c++-faq-lit...html#faq-36.7),
              you would use a similarly constructed extraction operator for a
              std::istream.
              >
              PS, you'll need to add some sort of delimeter between elements, whether
              a space, comma, new line, or whatever. Otherwise the unserializing
              won't be possible.
              >
              Cheers! --M
              Oh dear, this is going to be painful. If I have 10000 elements in my
              float vectors, that's a lot of delimitation.

              Thanks guys,
              Mark

              Comment

              • Jim Langston

                #8
                Re: serialize a structure

                "markww" <markww@gmail.c omwrote in message
                news:1156782612 .156650.255710@ 75g2000cwc.goog legroups.com...
                >
                mlimber wrote:
                >mlimber wrote:
                markww wrote:
                Gernot Frisch wrote:
                "markww" <markww@gmail.c omschrieb im Newsbeitrag
                news:1156773897 .989394.155270@ i3g2000cwc.goog legroups.com...
                Hi,
                >
                I have a data structure that looks like this:
                >
                struct MY_STRUCT {
                int x,y,z;
                string str1;
                string str2;
                vector<floatvfl oats1;
                vector<floatvfl oats2;

                std::ostream& operator << (std::ostream& os)
                {
                os << x << y << z << str1 << str1 << str2;
                os << vfloats1.size() ;
                for (int i=0; i<vflaots1.size ; ++i) os << vfloats1[i];
                os << vfloats2.size() ;
                for (int i=0; i<vflaots2.size ; ++i) os << vfloats2[i];
                return os;
                }

                }


                You might get into trouble with the strings, though, since some
                delimiter characters as '\n' within a std::string can't be read in
                this way.
                >
                Gernot, how would I read that back in?
                >
                As the FAQs say
                (http://www.parashift.com/c++-faq-lit...html#faq-36.7),
                you would use a similarly constructed extraction operator for a
                std::istream.
                >>
                >PS, you'll need to add some sort of delimeter between elements, whether
                >a space, comma, new line, or whatever. Otherwise the unserializing
                >won't be possible.
                >>
                >Cheers! --M
                >
                Oh dear, this is going to be painful. If I have 10000 elements in my
                float vectors, that's a lot of delimitation.
                It's 1000 characters which isnt' much.

                I would use spaces to delimit them since that works easily with reading them
                back in.

                Consider, also, that you don't actually need to store the size of the
                vectors.
                x y z
                string1
                string2
                vec11 vec12 vec13 vec14 vec15
                vec21 vec22 vec23 vec24 vec25 vec26 vec27

                Now in your operator<< you read 3 ints, throw the rest of the line away.
                read a line - store in string1
                read a line - strong in string2
                read a line - process through stringstream to get elements out
                std::string InputString;
                std::getline( Inputstream, InputString );
                std::stringstre am LineStream;
                LineStream << InputString;
                float Value;
                while ( LineStream >Value )
                vector1.push_ba ck( Value );
                there, that processes one vector. Now do the same for the second.

                Personally, I find the delimitators that istream works with the easiest to
                use.

                >
                Thanks guys,
                Mark

                Comment

                Working...