objectStream in C++?

Discussion in 'C++' started by Jens, Oct 28, 2004.

  1. Jens

    Jens Guest

    Hi

    Is there at class in C++ to make objectstreams or shall I make it yourself?

    Thanks
    Jens
    Jens, Oct 28, 2004
    #1
    1. Advertising

  2. "Jens" <> wrote in message
    news:4180be42$0$93475$...
    > Hi
    >
    > Is there at class in C++ to make objectstreams or shall I make it
    > yourself?
    >
    > Thanks
    > Jens


    A common solution is to define streaming operators for stream in and out of
    an object.

    For example,

    class A
    {
    [...]
    template <typename E, typename T>
    friend std::basic_ostream< E, T>& operator<<( std::basic_ostream< E, T>&
    os, const A& obj)
    {
    os << obj.x;
    return os;
    }

    template <typename E, typename T>
    friend std::basic_istream< E, T>& operator>>( std::basic_istream< E, T>&
    is, A& obj)
    {
    is >> obj.x;
    return is;
    }

    private:
    int x;
    };

    You have in STL file streams, string streams and you can find other kind of
    streams (for sockets for example).

    the usage is very simple:

    A obj1, obj2;
    std::cout << obj1 << obj2;

    Catalin

    P.S. The code is just a sketch. It may not compile.
    Catalin Pitis, Oct 28, 2004
    #2
    1. Advertising

  3. Jens

    Rolf Magnus Guest

    Jens wrote:

    > Hi
    >
    > Is there at class in C++ to make objectstreams or shall I make it
    > yourself?


    That depends on what an "objectstream" is.
    Rolf Magnus, Oct 28, 2004
    #3
  4. Jens

    Jens Guest

    Catalin Pitis wrote:

    >
    > "Jens" <> wrote in message
    > news:4180be42$0$93475$...
    >> Hi
    >>
    >> Is there at class in C++ to make objectstreams or shall I make it
    >> yourself?
    >>
    >> Thanks
    >> Jens

    >
    > A common solution is to define streaming operators for stream in and out
    > of an object.
    >
    > For example,
    >
    > class A
    > {
    > [...]
    > template <typename E, typename T>
    > friend std::basic_ostream< E, T>& operator<<( std::basic_ostream< E,
    > T>&
    > os, const A& obj)
    > {
    > os << obj.x;
    > return os;
    > }
    >
    > template <typename E, typename T>
    > friend std::basic_istream< E, T>& operator>>( std::basic_istream< E,
    > T>&
    > is, A& obj)
    > {
    > is >> obj.x;
    > return is;
    > }
    >
    > private:
    > int x;
    > };
    >
    > You have in STL file streams, string streams and you can find other kind
    > of streams (for sockets for example).
    >
    > the usage is very simple:
    >
    > A obj1, obj2;
    > std::cout << obj1 << obj2;
    >
    > Catalin
    >
    > P.S. The code is just a sketch. It may not compile.

    Hi

    My problem is to write a vector<float> to a file and later read the file
    into a vector<float>. Sorry I not where precise.

    Jens
    Jens, Oct 28, 2004
    #4
  5. "Jens" <> wrote in message
    news:4180d795$0$93463$...
    > Catalin Pitis wrote:
    >
    >>
    >> "Jens" <> wrote in message
    >> news:4180be42$0$93475$...
    >>> Hi
    >>>
    >>> Is there at class in C++ to make objectstreams or shall I make it
    >>> yourself?
    >>>
    >>> Thanks
    >>> Jens

    >>
    >> A common solution is to define streaming operators for stream in and out
    >> of an object.
    >>
    >> For example,
    >>
    >> class A
    >> {
    >> [...]
    >> template <typename E, typename T>
    >> friend std::basic_ostream< E, T>& operator<<( std::basic_ostream< E,
    >> T>&
    >> os, const A& obj)
    >> {
    >> os << obj.x;
    >> return os;
    >> }
    >>
    >> template <typename E, typename T>
    >> friend std::basic_istream< E, T>& operator>>( std::basic_istream< E,
    >> T>&
    >> is, A& obj)
    >> {
    >> is >> obj.x;
    >> return is;
    >> }
    >>
    >> private:
    >> int x;
    >> };
    >>
    >> You have in STL file streams, string streams and you can find other kind
    >> of streams (for sockets for example).
    >>
    >> the usage is very simple:
    >>
    >> A obj1, obj2;
    >> std::cout << obj1 << obj2;
    >>
    >> Catalin
    >>
    >> P.S. The code is just a sketch. It may not compile.

    > Hi
    >
    > My problem is to write a vector<float> to a file and later read the file
    > into a vector<float>. Sorry I not where precise.
    >
    > Jens


    Here is some code

    vector< float> vecToStore;
    // ... initialize it

    // Output to stream cout, with space delimiters
    copy ( vecToStore.begin ( ), vecToStore.end ( ),
    ostream_iterator<float> ( cout, " " ) );

    vector< float> vecToLoad;

    // Input from stream cin
    copy( istream_iterator<float>( cin), istream_iterator<float>(),
    vecToLoad.begin());

    Again, it is just a sketch.

    Catalin
    Catalin Pitis, Oct 28, 2004
    #5
  6. >
    > Here is some code
    >
    > vector< float> vecToStore;
    > // ... initialize it
    >
    > // Output to stream cout, with space delimiters
    > copy ( vecToStore.begin ( ), vecToStore.end ( ),
    > ostream_iterator<float> ( cout, " " ) );
    >
    > vector< float> vecToLoad;
    >
    > // Input from stream cin
    > copy( istream_iterator<float>( cin), istream_iterator<float>(),
    > vecToLoad.begin());
    >


    Should be

    copy( istream_iterator<float>( cin), istream_iterator<float>(),
    back_inserter(vecToLoad));

    Also the code above uses a text representation of the floating point
    numbers, which is good because its portable. But if you want a binary
    representation, try this

    vector< float> vecToStore;
    vector< float>::size_type size = vecToStore.size();
    out.write((char*)&size, sizeof size);
    if (size > 0)
    out.write((char*)&vecToStore[0], size*sizeof(float));

    vector< float> vecToLoad;

    vector< float>::size_type size;
    in.write((char*)&size, sizeof size);
    if (size > 0)
    {
    vecToLoad.resize(size);
    in.write((char*)&vecToLoad[0], size*sizeof(float));
    }

    Again untested.

    john
    John Harrison, Oct 28, 2004
    #6
    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. Peter Forster
    Replies:
    4
    Views:
    493
    Ryan Stewart
    Feb 27, 2004
  2. espresso

    ObjectStream issues

    espresso, Jun 15, 2004, in forum: Java
    Replies:
    4
    Views:
    498
    Dave Monroe
    Jun 19, 2004
  3. Gordon Beaton
    Replies:
    5
    Views:
    335
    Robert Worrall via JavaKB.com
    Nov 1, 2005
  4. ZelluX
    Replies:
    5
    Views:
    1,071
    Roedy Green
    Oct 24, 2007
Loading...

Share This Page