By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,849 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,849 IT Pros & Developers. It's quick & easy.

Streambuf and streams

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Tue, 31 Aug 2004 15:29:03 GMT, "Henry" <no**********@hotmail.com>
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/IOStrea...cerpt.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
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.