streambuf/iostream weirdness (bug?)

Brent Verner brent@linux1.org
Tue Apr 25 10:21:00 GMT 2000


Hi,

  I've ran into a problem with a little piece of code that I'm 
working on that subclasses std::streabuf and std::iostream to
use C's write() on a file descriptor specified in its ctor.
Anyway, this all works ok until I put an std::endl to the
stream, then nothing else will be put to it. It is _very_
possible that I've screwed up my code, but if one of the gurus
on the list could examine the situation, I'd appreciate it. If
I've committed some greivous oversight in my attempt, I'd
appreciate being shown my error. (code is below)

relevant info:
  gcc version 2.96 20000417 (experimental)
  libstdc++ from CVS on 24 Apr 2000

Thanks,
  Brent

==============================================================
#include <cstdio>
#include <iostream>
#include <streambuf>

extern "C" {
  int write( int fd, const char* buf, int len );
  int  read( int fd, void* buf, int len );
}

class FDStreamBuffer : public std::streambuf {
protected:
  int fd;
public:
  FDStreamBuffer( int cfd ) : fd( cfd ) { }
protected:
  virtual int overflow( int arg ){
    if( arg != EOF ){
      char ch = arg;
      if( write( fd, &ch, 1 ) != -1 ){
        return EOF;
      }
    }
    return arg;
  }
  virtual std::streamsize xsputn( const char* cha,
                            std::streamsize len ){
    return write( fd, cha, len );
  }
};

class FDIOStream : public std::iostream {
protected:
  FDStreamBuffer fdbuf;
public:
  FDIOStream( int fd ) : std::iostream(&fdbuf), fdbuf( fd ) { }
};

int
main()
{
  FDIOStream fdout(1);
  fdout << "break" << std::endl;
  fdout << "down";
  std::cout << std::endl << "std::endl put to std::cout works" << std::endl;
  return 0;
}


More information about the Libstdc++ mailing list