 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Christopher Benson-Manica Guest
|
Posted: Thu Feb 26, 2004 5:00 pm Post subject: 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.
|
|
| Back to top |
|
 |
Christopher Benson-Manica Guest
|
Posted: Thu Feb 26, 2004 5:02 pm Post subject: Re: streambuf not flushing, apparently |
|
|
Christopher Benson-Manica <ataru (AT) nospam (DOT) cyberspace.org> spoke thus:
| Quote: | 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.
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Thu Feb 26, 2004 5:05 pm Post subject: Re: streambuf not flushing, apparently |
|
|
* Christopher Benson-Manica <ataru (AT) nospam (DOT) cyberspace.org> schriebt:
| Quote: | Christopher Benson-Manica <ataru (AT) nospam (DOT) 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.
|
|
| Back to top |
|
 |
Christopher Benson-Manica Guest
|
Posted: Thu Feb 26, 2004 5:10 pm Post subject: Re: streambuf not flushing, apparently |
|
|
Alf P. Steinbach <alfps (AT) start (DOT) no> spoke thus:
| Quote: | 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.
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Thu Feb 26, 2004 5:16 pm Post subject: Re: streambuf not flushing, apparently |
|
|
* Christopher Benson-Manica <ataru (AT) nospam (DOT) cyberspace.org> schriebt:
| Quote: |
Trust me, streams are
looking like a nice alternative, if I can get them to work.
|
Heh... ;-)
LOL.
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Thu Feb 26, 2004 7:43 pm Post subject: Re: streambuf not flushing, apparently |
|
|
"Christopher Benson-Manica" <ataru (AT) nospam (DOT) cyberspace.org> wrote
| Quote: | 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.
| Quote: | }
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
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
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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|