[streams] surplus output generated

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

Sven Heithecker escribió:
Hi all,
the code below using boost::iostream produces unwanted surplus output:
streamsize write(const char *s, streamsize n) { cout << "Write: " << s; return(n); }
My guess is you should only read up to n chars. Try copying n chars from s, and outputing those. Agustín K-ballo Bergé.-

Am Samstag, 12. September 2009 schrieb Agustín K-ballo Bergé:
Sven Heithecker escribió:
Hi all,
the code below using boost::iostream produces unwanted surplus output:
streamsize write(const char *s, streamsize n) { cout << "Write: " << s; return(n); }
My guess is you should only read up to n chars. Try copying n chars from s, and outputing those.
Hi, brilliant idea - works like a charm ! Thanx for help. Regards, Sven -- Sven Heithecker GPG Fingerprint: 52F7 06C2 BF51 96CE DFF7 CC89 1A6B DFD5 BEC2 02A6
participants (2)
-
Agustín K-ballo Bergé
-
Sven Heithecker