"std::endl or "\n" "

A

andrew.smith.cpp

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

Thanks
 
P

pauldepstein

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

Thanks

Not all differences are visible. The difference is that std::endl
flushes the stream's buffer and \n doesn't.
Also \n is a character and I think std::endl is a function. (Not 100%
sure of my 2nd assertion but very sure of my first.)

Paul Epstein
 
A

andrew.smith.cpp

Not all differences are visible.  The difference is that std::endl
flushes the stream's buffer and \n doesn't.
Also \n is a character and I think std::endl is a function. (Not 100%
sure of my 2nd assertion but very sure of my first.)

Paul Epstein

Thanks Paul ;-)
 
R

Rolf Magnus

Not all differences are visible. The difference is that std::endl
flushes the stream's buffer and \n doesn't.

Yes. stream << endl is equivalent to stream << '\n' << std::flush.
Also \n is a character and I think std::endl is a function. (Not 100%
sure of my 2nd assertion but very sure of my first.)

Both are correct.
 
J

James Kanze

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.)
 
J

James Kanze

On Mar 29, 12:48 pm, (e-mail address removed) wrote:
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.)
 
O

Obnoxious User

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.
 
A

André Castelo

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?
 
E

Erik Wikström

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 its contents is written to the file/
console associated with it. Should something happen to your program
before the buffers are flushed it is possible that the contents in the
buffers is lost (i.e. not written to file).
 
R

Rolf Magnus

André Castelo said:
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.
 
J

Juha Nieminen

André Castelo said:
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.
 
J

James Kanze

André Castelo a écrit :
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.
 
J

James Kanze

On 2008-03-29 15:03, André Castelo wrote:
When you flush the stream buffer its contents is written to
the file/ console associated with it. Should something happen
to your program before the buffers are flushed it is possible
that the contents in the buffers is lost (i.e. not written to
file).

Just a nit, but that's not strictly true. When you flush a
stream buffer, its contents are transfered to the OS. In most
cases, they will not immediately be written to the file (but
other processes on the system will see them as though they
were). There is a (usually very small) window of time after the
flush where you can loose the data if the OS crashes.

For most applications, it's not an issue, but if you need
transactional integrity, it can be.
 

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,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top