[lug] simple iostream bug?

Jonathan Briggs zlynx at acm.org
Thu Mar 14 17:03:16 MST 2002

This is because you are sending a ostream reference (the return value of 
TextHeader) into an ostream and it appears to be converting it into an 
unsigned long.  This is probably because iostreams return true if the 
stream state is good and false if the stream state is bad (so you can do 
while( cout ), or similar things.)

Just do TextHeader( cout ).

If you want to be able to do cout << TextHeader (as a manipulator, like 
how endl is used) you'll need to do some more magic (Bear with me, I'm 
not sure I remember this perfectly.  You may need to consult 

Create a manipulator class/struct:  This can be anything, it doesn't 
have to be part of any hierarchy.
If it takes an argument, you make the constructor take the arguments. 
 If it doesn't take arguments, I think you need to create a global 
instance of your class (I _think_ endl is an instance of something.)
Make a non-member function for ostream& operator<<(ostream&, 

D. Stimits wrote:

>I'm trying to do something very very simple, and I know this should
>work, but it doesn't. I'm thinking there is a bug in the iostream
>library, maybe someone could tell me what I'm doing wrong here ("it
>shouldn't happen"). Basically it is just a function to take a reference
>to an ostream, insert some text, and return it by reference. In this
>case it is cout. The code:
>#include <iostream>
>ostream& TextHeader( ostream& web_output )
>   web_output << "Content-type: text/html\n" << endl;
>   return web_output;
>int main()
>   cout << TextHeader( cout );
>   cout << "<HTML><HEAD><TITLE>Testing</TITLE></HEAD>\n";
>   cout << "<BODY><P>Testing</P></BODY></HTML>" << endl;
>   return 0;
>What I expect to see:
>Content-type: text/html
>What I actually see:
>Content-type: text/html
>Notice the extra "0xffffffff" leading a line. I have tried variations
>with flush(), and assertions on stream state being good. No luck, I
>cannot get the 0xffffffff to disappear. What is interesting is that if I
>modify the TextHeader function to simply pass along the reference to the
>stream, without any modifications, insertions, operations...no changes
>at all...the 0xffffffff still occurs. It appears that passing cout by
>reference is sufficient to embed 0xffffffff, nothing else is required.
>Does this seem to be a bug to anyone else? Or maybe there is something
>more subtle going on?
>One detail: In my setup, the TextHeader method is actually in a header
>file all by itself, and I #include it at the top of main.cxx.
>D. Stimits, stimits at idcomm.com
>Web Page:  http://lug.boulder.co.us
>Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug

More information about the LUG mailing list