Re: <Help> Simple Pause Needed.

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 10/02/03


Date: Thu, 02 Oct 2003 20:02:44 GMT


"Jonathan Mcdougall" <jonathanmcdougall@DELyahoo.ca> wrote in message
news:k5%eb.5830$yV2.104971@weber.videotron.net...

> > > I prefer using std::endl since it is clearer than a bunch of \n.
> > > It is also supposed to flush the buffer, but flushing the buffer
> > > does not necessrily mean that the data is written.
> >
> > Again, Eh?
>
> I prefer using std::endl since it is clearer than a bunch of \n.

I disagree. Any C++ programmer will know what '\n' means.
As readily as what e.g. 'int' means. I suppose it can
vary among people, but I find '\n' simpler reading.

> What's more, since std::cout is buffered, std::end flushes it.

Yes, I know. That's 'std::endl' :-)

> That
> does not mean the output will automatically get on the stdout though,

Where did you hear that? What do you think 'flush' means,
in the context of an output stream?

> since operating systems usually provide some buffering themselves
> (this applies to all streams).

Now you're outside the language. Nothing to do with
flushing a stream. And your generalization 'all streams'
is incorrect.

>To make sure the data gets written,
> you should use implementation-specific functions which deal with
> the operating system's buffer.

I vehemently disagree. If this were the case, C++ would
fail in its role as a platform independent language.

> But usually, flushing the buffer is enough.

"Usually", flushing a buffer is not necessary at all.

>Note that std::cout
> flushes the buffer when it is destroyed

During 'cout's destruction, it (the stream) is flushed.

>(ie at the end of the
> program), but you may have no time to see the output

Huh?

So you're saying that if I run:

#include <iostream>
int main()
{
    std::cout << "Hello world\n";
    return 0;
}

.. that the output might disappear immediatly after
appearing on the output device to which 'cout' is
attached? I don't think so. An IDE might interfere
with a 'screen' output, but that's outside the language.

>(in case
> of outputting to stdout when there is a screen on your system)

Absolutely not true.

> so make sure you flush the buffer yourself when you are
> finished writing.

Unnecessary, unless you require that the output appear
*immediately* after inserting data into the stream.
And it's not even necessary if a 'cout' insertion
is followed by a 'cin' extraction, since by defintion,
a 'cin' extraction will first cause 'cout' to be flushed
if necessary (look up 'tie()').

>
> Finally, '\n' may flush the buffer or not,

'\n' is not an operation. It's a value, that's all.
It cannot 'do' anything. A function might behave
in a particular way upon encountering it, this
behavior perhaps causing a flush.

>this is not defined
> (just as it may flush it with any character).
>
> Interesting note that I found in my researches, \n is
> translated into its equivalent depending on the platform.

That is common knowledge. It's a feature of a 'text mode'
stream, well documented.

> For example, when writing to a text file, Windows needs a
> \r\n and Mac gets a \r.

I know this.

No offense, but I think you're suffering from a few
misconceptions.

-Mike



Relevant Pages

  • Re: Sending XML data via socket
    ... I looked int a link you have provided. ... // Get a stream object for reading and writing ... Resize the buffer, put in the byte we've just ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Trouble with custom InputStream being used by Readers
    ... wrong with my Stream. ... He'll fill his buffer. ... private final Object lock = new Object; ... public synchronized int read ...
    (comp.lang.java.programmer)
  • Re: Threadpool doubt
    ... Flush will only flush the contents of the current stream. ... > int mWorkerThreads; ... > ThreadPool.GetMaxThreads;> ThreadPool.GetAvailableThreads(out aWorkerThreads, out> aPortThreads); ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: I/O buffering
    ... Flushing the contents of the buffer happens only when the FileStream ... specific methods on the instance that can flush data, ... No. I'm speaking strictly of the FileStream instance to which I referred in that paragraph. ... The first three are specifically documented to flush the stream, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Why dont characters in a file get overwritten?
    ... Why didn't code f1.writeoverwrite characters inside ... f2.writewrites its argument value to the buffer in the ... argument to a stream of bytes and writes that stream of bytes to the ... and 2) when it is told to, such as when its flush method is invoked. ...
    (comp.lang.java.programmer)