Streambuf and streams

H

Henry

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;
}
 
T

tom_usenet

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top