ostream_iterator

A

Aman

Hi,
please consider the following prog (SunOS 5.8,g++ compiler)
when I ran it through the debugger , I found that the program doesn't
output till either the "\n" or the exit .
(debugger ..Single stepping until exit from function _start ...)
what does reaching exit or \n have to do with cout or ostream_iterator
?


#include <iostream>
#include <string>
#include <iterator>
using namespace std ;

int main()
{
ostream_iterator<string> out(cout , " ") ;
*out ="hello" ;
out++ ;
*out = "world" ;
out++ ;
*out = "\n" ;
out++ ;
*out = "again" ;

}

regards,
 
M

Mike Wahler

Aman said:
Hi,
please consider the following prog (SunOS 5.8,g++ compiler)
when I ran it through the debugger , I found that the program doesn't
output till either the "\n" or the exit .
(debugger ..Single stepping until exit from function _start ...)
what does reaching exit or \n have to do with cout or ostream_iterator
?


#include <iostream>
#include <string>
#include <iterator>
using namespace std ;

int main()
{
ostream_iterator<string> out(cout , " ") ;
*out ="hello" ;
out++ ;
*out = "world" ;
out++ ;
*out = "\n" ;
out++ ;
*out = "again" ;

}

Output is not guaranteed to be delivered to the
device attached to a stream until a newline is
inserted into the stream, or the stream is explicitly
flushed, or the stream object is destroyed, or if
it is a file stream, the file is closed.

This is by design.

A 'special case' exists with regard to 'cin' and 'cout':
An insertion to 'cout' will first cause an automatic 'flush'
of 'cin' if any data remain in its stream buffer (see 'tie()')

HTH,
-Mike
 
A

Aman

Thanks , makes sense.
Is there anyplace where this behaviour is documented ?
url / book reference would be good.

regards,
Aman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top