Boost logo

Boost Users :

Subject: [Boost-users] [streams] surplus output generated
From: Sven Heithecker (sven.heithecker_at_[hidden])
Date: 2009-09-11 17:23:00


Hi all,

the code below using boost::iostream produces unwanted surplus output:

Write: this ist a test output split into two parts
Print: this flushes previous output
Write: another test output
t split into two parts

The last line is a fragment of the first line which should not be
there.

I am using boost 1_39, gcc 4.3.2 [gcc-4_3-branch revision 141291]

What am I doing wrong here ?

BTW: the code is a stripped demo code. Originally, "true" devices are
used instead of the dummy "0", and I use some home-brewn classes
(instead of just "string") which require specialized output handling
depending on the used device, therefore the direct call into the
stream object. However, this makes no difference.

Regards,
Sven

---------------------------------------------------------------------
#include <iostream>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/concepts.hpp>

using namespace std;
namespace io = boost::iostreams;

// "demo" strem sink

class CTestSink: public io::sink
 {
  public:
  CTestSink(int dummy) {} // iostreams requires constructor argument

  public:
  streamsize write(const char *s, streamsize n)
   {
    cout << "Write: " << s;
    return(n);
   }

  void Print(const string &str) const
   {
    cout << "Print: " << str << endl;
   }
 };

// the real stream

typedef io::stream<CTestSink> CTestOStream;

// specialized output for "string" objects

CTestOStream &operator<<(CTestOStream &stream, string &str)
 {
  // make sure previous output is flushed
  // (not required in this example)
  stream.flush();

  stream -> Print(str); // specialized output handling
  return(stream);
 }

int main()
 {
  // dummy "0" required by boost:iostreams
  CTestOStream testostream(0);

  testostream << "this ist a test output ";
  testostream << "split into two parts" << endl;

  string str("this flushes previous output");
  testostream << str;

  testostream << "another test output " << endl;
 }

-- 
Sven Heithecker
GPG Fingerprint: 52F7 06C2 BF51 96CE DFF7 CC89 1A6B DFD5 BEC2 02A6

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net