473,412 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

streambuf not flushing, apparently

Yes, it's me again, with my dear, dear friends std::streambuf and
std::ostream... On the bright side, the code compiles. On the gloomy
side, I can't seem to get overflow() or xsputn() called...

class TWFileBuf : public std::streambuf
{
private:
bool Open( name ) {/* debug printf - does show up */}

protected:
TLSFile lsf;
virtual int_type overflow(int_type c)
{
// debug printf omitted - it does NOT show up
if( lsf.Write(c) ) {
return c;
}
return EOF;
}
virtual std::streamsize xsputn(const char *s, std::streamsize num)
{
// debug printf omitted - it does NOT show up
lsf.Write(s);
return(strlen(s));
}

public:
TWFileBuf( const char* name ) {Open(name);}
};

class
TLogBuf : public TWFileBuf
{
protected:
virtual std::streamsize xsputn(const char *s, std::streamsize num)
{ /* do complicated stuff - printf here doesn't show up */ }

public:
TLogBuf( name ) : TWFileBuf(name) {}
};

template <class buftype>
class TWFileStreamBase : public std::ostream
{
protected:
buftype buf;

public:
TWFileStreamBase( const char* name ) :
buf(name),std::ostream(&buf) {}
};

typedef TWFileStream<TLogBuf> TDebugLogFile;

Any ideas on what's still wrong with this code.................?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #1
5 1928
Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus:
typedef TWFileStream<TLogBuf> TDebugLogFile;


I should add that I'm using a TDebugLogFile like so:

dbglog << "Hello, world!" << flush; // is flush what I want?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #2
* Christopher Benson-Manica <at***@nospam.cyberspace.org> schriebt:
Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus:
typedef TWFileStream<TLogBuf> TDebugLogFile;


I should add that I'm using a TDebugLogFile like so:

dbglog << "Hello, world!" << flush; // is flush what I want?

I don't know, but I do know that the prudent thing to do if you find
yourself concerned with the innards of streams, is to _avoid_ streams.

Streams are extremely inefficient and ugly as hell (as evidenced by your
confusion over some detail or other) and not even as type safe as advertised.

Forget them.

Jul 22 '05 #3
Alf P. Steinbach <al***@start.no> spoke thus:
Streams are extremely inefficient and ugly as hell (as evidenced by your
confusion over some detail or other) and not even as type safe as advertised.


Uglier than the alternative, which is

TLSFile f;

f.Print( "The number of ways I like printf is %u", 0 );

? Or better yet, the other class I want to be like a stream...

Socket s;

s.Send( bprf("Wow, I hate this function in %d ways!",666) );

where bprf() is like printf() but gives you a temporary buffer that
times out after a finite amount of time. Trust me, streams are
looking like a nice alternative, if I can get them to work.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #4
* Christopher Benson-Manica <at***@nospam.cyberspace.org> schriebt:

Trust me, streams are
looking like a nice alternative, if I can get them to work.


Heh... ;-)

LOL.

Jul 22 '05 #5

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c1**********@chessie.cirr.com...
Yes, it's me again, with my dear, dear friends std::streambuf and
std::ostream... On the bright side, the code compiles. On the gloomy
side, I can't seem to get overflow() or xsputn() called...

class TWFileBuf : public std::streambuf
{
private:
bool Open( name ) {/* debug printf - does show up */}

protected:
TLSFile lsf;
virtual int_type overflow(int_type c)
{
// debug printf omitted - it does NOT show up
if( lsf.Write(c) ) {
return c;
}
return EOF;
}
virtual std::streamsize xsputn(const char *s, std::streamsize num)
{
// debug printf omitted - it does NOT show up
lsf.Write(s);
return(strlen(s));
This is wrong, you're assuming that s is a null terminated string, which
isn't necessarily the case.

Doesn;t explain why this function isn't called at all however.
}

public:
TWFileBuf( const char* name ) {Open(name);}
};

class
TLogBuf : public TWFileBuf
{
protected:
virtual std::streamsize xsputn(const char *s, std::streamsize num)
{ /* do complicated stuff - printf here doesn't show up */ }

public:
TLogBuf( name ) : TWFileBuf(name) {}
};

template <class buftype>
class TWFileStreamBase : public std::ostream
{
protected:
buftype buf;

public:
TWFileStreamBase( const char* name ) :
buf(name),std::ostream(&buf) {}
};

typedef TWFileStream<TLogBuf> TDebugLogFile;

Any ideas on what's still wrong with this code.................?


Can't see anything wrong with it.

I would use a debugger to step in the code when you do << to see what is
really happening. Because you are doing unbuffered I/O you shouldn't need to
use flush, output should happen immediately.

Not all compilers implement the standard library correctly, some things you
might check are.

That the stream error state isn't set, try calling clear() in the
TWFileStreamBase constructor.

You are using unbuffered output, try saying so explicitly by calling
buf.setp(0,0) in TWFileStreamBase constructor.

Try checking that your overirides really are overriding the streambuf
function, check the streambuf header file to see if the argument types for
overflow and xsputn match the ones you are using.

But I think a good debugger is your main tool.

Actually the code above cannot be the correct code, TWFileStream<TLogBuf>
does not have a default constructor. That makes me think that the problem is
that you've got the stream into an error state, when that happens no I/O
will occur until you call clear().

john
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Viktor Lundström | last post by:
Hi! I was planning to wrap a socket inside an iostream, to achieve something like this: TCPSocket s(..); s << "Hello!" << endl; Information on the web seems to be a bit scarce on how to do...
3
by: Christopher Benson-Manica | last post by:
This is starting to seem ridiculous to me :( #include <streambuf> #include <iostream> class TWFileStream : public std::streambuf { private: char cbuf;
9
by: Fred Ma | last post by:
Hello, I posted previously under the thread: How to break this up into streambuf/ostream I've asked our library to get "C++ IOStreams and Locales..." by A. Langer et al. Meantime, I've...
9
by: Marcin Kalicinski | last post by:
Hi, I have a set of C-like functions for file access (like fopen, fwrite, fread, fseek etc.). But I want to access the files using C++ stream, not these functions. What I probably need to do is...
2
by: Raf256 | last post by:
Hello, my custom streambuf works fine with output via << and with input via .get() but fails to input via >> or getline... any idea why? -------------------- A custom stream buffer (for...
7
by: smith4894 | last post by:
Hello all, I'm working on writing my own streambuf classes (to use in my custom ostream/isteam classes that will handle reading/writing data to a mmap'd file). When reading from the mmap...
0
by: tryptik | last post by:
All- I have been looking at all the info I can find on deriving streambuf objects, but I need some advice. I wish to derive a custom streambuf that filters out c++ style comments. I have...
4
by: rakesh.usenet | last post by:
For a particular application of mine - I need a simulation of byte array output stream. * write data onto a stream * getback the contiguous content as an array later for network transport. ...
3
by: Raymond Martineau | last post by:
I have the following code segment for a class intended to split output between cout and a file: class SplitStream : public std::streambuf { std::streambuf *x; public: SplitStream() {
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.