468,291 Members | 1,423 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,291 developers. It's quick & easy.

"std::endl or "\n" "

hi,
whts the difference between the std::endl or "\n" ?
because both do the same work

Thanks
Mar 29 '08 #1
9 3003
On Mar 29, 12:48*pm, andrew.smith....@gmail.com wrote:
hi,
whts the difference between the std::endl or "\n" *?
because both do the same work

Thanks
I found nothing difference between both at least in my program.
I also want to know that.
Mar 29 '08 #2
On 29 mar, 05:48, andrew.smith....@gmail.com wrote:
whts the difference between the std::endl or "\n" ?
because both do the same work
std::endl flushes the buffer, "\n" doesn't. If you don't know
what you're doing, and don't want to think about it, use
std::endl. It will make debugging a lot easier. If you're
outputting a lot of short lines in a sequence, however, you
probably want to use "\n", since flushing can be a fairly
expensive proposition. (My general rule is to use "\n" in loops
or short sequences of code, but to make sure that there is an
std::endl or an explicit call to flush at the end of the
function or after the loop.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 29 '08 #3
On 29 mar, 07:22, pauldepst...@att.net wrote:
On Mar 29, 12:48 pm, andrew.smith....@gmail.com wrote:
whts the difference between the std::endl or "\n" ?
because both do the same work
Not all differences are visible. The difference is that std::endl
flushes the stream's buffer and \n doesn't.
Which is a very visible difference if your program crashes
immediately after, or if you don't do anything to flush the
buffer immediately.

Ideally, you'd consider both and choose the appropriate one each
time. Practically, for most smaller programs, just using
std::endl everywhere is the simplest solution. (If you're
generating a file of a couple of million lines, however, I
wouldn't recommend it.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 29 '08 #4
On Mar 29, 12:48 pm, andrew.smith....@gmail.com wrote:
whts the difference between the std::endl or "\n" ?
because both do the same work
Not all differences are visible. The difference is that std::endl
flushes the stream's buffer and \n doesn't.
What are the advantages of flushing the stream buffer?? Does it mean
that the ocasional '\n' from input will be discarded or am i missing
something?
Mar 29 '08 #5
On Sat, 29 Mar 2008 07:03:29 -0700, André Castelo wrote:
On Mar 29, 12:48 pm, andrew.smith....@gmail.com wrote:
whts the difference between the std::endl or "\n" ? because both
do the same work
Not all differences are visible. The difference is that std::endl
flushes the stream's buffer and \n doesn't.

What are the advantages of flushing the stream buffer?? Does it mean
that the ocasional '\n' from input will be discarded or am i missing
something?
When you flush the stream buffer you're telling it to write what
ever it holds and clear it. If it doesn't get flushed, you won't see it
on your terminal or in your file.

--
OU
Mar 29 '08 #6
André Castelo wrote:
On Mar 29, 12:48 pm, andrew.smith....@gmail.com wrote:
whts the difference between the std::endl or "\n" ?
because both do the same work
Not all differences are visible. The difference is that std::endl
flushes the stream's buffer and \n doesn't.

What are the advantages of flushing the stream buffer?? Does it mean
that the ocasional '\n' from input will be discarded or am i missing
something?
No. Writing I/O is usually faster if you write several bytes at once instead
of sending each to its target seperately, be it for file I/O or the
standard output streams, since there is (on most systems) a relatively
large fixed overhead for writing something. Therefore, the streams buffer
data internally and then send it to its target as a block, either when the
buffer is full, or when you explicitly flush the stream.

Mar 29 '08 #7
André Castelo wrote:
What are the advantages of flushing the stream buffer?? Does it mean
that the ocasional '\n' from input will be discarded or am i missing
something?
If you are, for example, writing thousands of lines of text to a file,
using "\n" instead of std::endl is much faster because the stream is not
flushed after each line.

If you are printing a few lines of text to standard output, std::endl
may be a better idea because it forces the text to be flushed before the
program continues.
Mar 30 '08 #8
André Castelo a écrit :
On Mar 29, 12:48 pm, andrew.smith....@gmail.com wrote:
whts the difference between the std::endl or "\n" ?
because both do the same work
Not all differences are visible. The difference is that std::endl
flushes the stream's buffer and \n doesn't.
What are the advantages of flushing the stream buffer?? Does
it mean that the ocasional '\n' from input will be discarded
or am i missing something?
The data isn't actually output until you flush the buffer. If
someone else is waiting for it, they wait. If your program
crashes, it never gets written.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 30 '08 #9
James Kanze wrote:
The data isn't actually output until you flush the buffer.
Except if the buffer gets full. (Or you close the file handle, or...)
Mar 30 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Filipe Sousa | last post: by
9 posts views Thread by Richard Lionheart | last post: by
9 posts views Thread by Xian | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.