Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does new line character also flush the buffer?

Tags:

c++

flush

endl

I understand that questions like, difference between endl and \n have been answered many times on SO. But they only mention that endl is able to flush the buffer onto the stdout, while \n, does not.

So, what I understand by buffer being flushed is that, the input given is stored in a buffer, and is passed onto the stdout only when it comes across endl, or some explict flush functions. If so, I expected that the following code :

#include <iostream>
#include <unistd.h>

int main(void)
{
    std::cout << "Hello\nworld";
    sleep(2);
    std::cout << std::endl;

    return 0;
}

To display:

After 2 seconds

Hello
World

But the actual output was:

Hello

After 2 seconds

World

Why is it so ?

shouldn't \n also be stored in the buffer and only when endl is encountered buffer is to be flushed/displayed onto the stdout, but from what I observe \n is acting the same way as endl.

like image 885
CaptainDaVinci Avatar asked Feb 24 '17 04:02

CaptainDaVinci


People also ask

Does STD Endl flush the buffer?

Performance depends on the underlying stream buffer. std::endl writes a newline to the output stream and flushes it. In most cases, the developer doesn't need to flush. Flushing operation is expensive, because it involves a system call.

What does it mean by flushing the buffer?

A buffer flush is the transfer of computer data from a temporary storage area to the computer's permanent memory. For instance, if we make any changes in a file, the changes we see on one computer screen are stored temporarily in a buffer.

What does it mean to flush a buffer in C?

Clearing input buffer in C/C++ The function fflush(stdin) is used to flush or clear the output buffer of the stream. When it is used after the scanf(), it flushes the input buffer also. It returns zero if successful, otherwise returns EOF and feof error indicator is set.

What is new line character in C++?

The \n Character The other way to break a line in C++ is to use the newline character — that ' \n ' mentioned earlier. This is line one.


1 Answers

Converting comments into an answer.

It depends on where cout is going. If it goes to a terminal ('interactive device'), then it can't be fully buffered — it is usually line buffered, meaning that characters appear after a newline is printed, or could in theory be unbuffered. If it is going to a pipe or file or other non-interactive destination, the endl forces the data out even if the stream is fully buffered, as it usually will be.

I also wanted to know if I provided neither new line character nor endl, will the output be displayed on the stdout once it reaches the end of the program, I know it does for terminal, but is it applicable to all types of stdout?

Yes, when the file stream is closed at the (normal) end of the program, pending output will be flushed. It'll also be flushed when the buffer is full. If the program aborts, pending output usually won't be flushed.

like image 199
Jonathan Leffler Avatar answered Oct 18 '22 03:10

Jonathan Leffler