Streambuf and streams

Discussion in 'C++' started by Henry, Aug 31, 2004.

  1. Henry

    Henry Guest

    Hello!
    Im trying to get a memstream object to work but it seems that I miss
    something!
    My expectation is to have one stream for input and another for output!
    I also expect the streambuf to handle the allocation and deallocation of
    memory automaticly!
    Is it possible?
    I have some C++ books but they all has poor coveridge on this topic!

    Any examples on this topic!
    ..or help!
    Here is my snippet!
    It compiles and executes but the output is 'Count 0'
    -----------------------------------------------------------
    class CMemStream : public basic_streambuf<UCHAR,char_traits<UCHAR> >
    {
    public:
    typedef basic_istream<UCHAR, char_traits<UCHAR> > In;
    typedef basic_ostream<UCHAR, char_traits<UCHAR> > Out;
    };

    void Test()
    {
    CMemStream b;
    CMemStream::Out o(&b);
    CMemStream::In i(&b);
    o << 0x41;
    cout << "Count " << i.gcount() << endl;
    }

    -----------------------------------------------------------
    ...Henry
    Henry, Aug 31, 2004
    #1
    1. Advertising

  2. Henry

    tom_usenet Guest

    On Tue, 31 Aug 2004 15:29:03 GMT, "Henry" <>
    wrote:

    >Hello!
    >Im trying to get a memstream object to work but it seems that I miss
    >something!
    >My expectation is to have one stream for input and another for output!
    >I also expect the streambuf to handle the allocation and deallocation of
    >memory automaticly!
    >Is it possible?
    >I have some C++ books but they all has poor coveridge on this topic!
    >
    >Any examples on this topic!
    >.or help!


    Lots of info here (and you should buy the book)
    http://www.langer.camelot.de/IOStreams/Excerpt/excerpt.htm#Main

    >Here is my snippet!
    >It compiles and executes but the output is 'Count 0'
    >-----------------------------------------------------------
    >class CMemStream : public basic_streambuf<UCHAR,char_traits<UCHAR> >
    >{
    >public:
    >typedef basic_istream<UCHAR, char_traits<UCHAR> > In;
    >typedef basic_ostream<UCHAR, char_traits<UCHAR> > Out;
    >};
    >
    >void Test()
    >{
    > CMemStream b;
    > CMemStream::Out o(&b);
    > CMemStream::In i(&b);
    > o << 0x41;
    > cout << "Count " << i.gcount() << endl;
    >}


    You have a couple of problems. First of all, streams don't necessarily
    work well with UCHAR (due to char_traits<UCHAR> possibly being
    undefined). You should probably stick to char. Secondly, your
    CMemStream doesn't actually do anything - you need to override virtual
    functions like overflow, sync, underflow, etc. in order for it to have
    any functionality.

    But all this is quite hard; an easier way is to use a library, such
    as:
    http://home.comcast.net/~jturkanis/iostreams/
    (it is being reviewed for inclusion in the boost libraries at the
    moment, www.boost.org)

    Tom
    tom_usenet, Aug 31, 2004
    #2
    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 Jansson
    Replies:
    1
    Views:
    511
    David Rubin
    Nov 8, 2004
  2. Johannes Barop
    Replies:
    1
    Views:
    433
    JH Trauntvein
    Dec 29, 2005
  3. Raf256
    Replies:
    2
    Views:
    371
    Raf256
    May 25, 2006
  4. Christopher Pisz
    Replies:
    2
    Views:
    598
    James Kanze
    Dec 12, 2007
  5. mathieu

    exception and streambuf

    mathieu, Feb 24, 2011, in forum: C++
    Replies:
    4
    Views:
    569
Loading...

Share This Page