Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calls to flush cout are ineffective

Tags:

c++

flush

cout

I am attempting to have the cout buffer flush to view a string before I manipulate it. Ive attempted to flush the buffer with calls to both std::flush() and std::cout.flush() but neither actually flush my output.

Only a call to std::endl has successfully flushed the buffer for me.

Here is my code

std::istringstream stm (game.date());
int day, month, year;
char delim = '/';

std::cout << "Date before: " << game.date() << std::flush; // first flush attempt
std::cout.flush();  // second flush attempt doesnt work
//std::cout << std::endl;   // if this is executed the buffer will flush

// Split date string into integers for comparison
stm >> month >> delim;
stm >> day >> delim;
stm >> year >> delim;

std::cout << "Date after: " << " | " << month << "/" << day << "/" << year << std::endl;

Here is my output
Date after: | 1/31/13
Date after: | 3/21/12
Date after: | 11/11/11
Date after: | 10/1/10
Date after: | 1/2/12

So as you can see the first call to cout isnt ever flushed but as I said before the buffer will successfully flush with endl, which calls flush itself. I am currently running Ubuntu 12.04 with VirtualBox on my host macbook pro running Mountain Lion.

Is there anything I may be doing wrong in my flush calls or is this potentially a system issue?

like image 349
Tyler Avatar asked Oct 20 '13 22:10

Tyler


People also ask

Do you need to flush cout?

An explicit flush of std::cout is also necessary before a call to std::system, if the spawned process performs any screen I/O (a common example is std::system("pause") on Windows).

What is flush in C++ cout?

In C++, we can explicitly be flushed to force the buffer to be written. Generally, the std::endl function works the same by inserting a new-line character and flushes the stream. stdout/cout is line-buffered that is the output doesn't get sent to the OS until you write a newline or explicitly flush the buffer.

Why you shouldn t use endl?

There is rarely a need for std::endl , and getting in the habit of using it will lead to mysteriously slow code. Just use '\n' unless you know you need to flush the buffer. Save this answer.

Why do we need flush C++?

C++ manipulator flush is used to synchronize the associated stream buffer with its controlled output sequence. For the stream buffer, objects that implement intermediate buffers, flush function is used to request all characters written to the controlled sequence.


1 Answers

Both std::cout << flush; and std::cout.flush(); will flush std::cout.

It looks as if your code inserts a carriage return (\r) into the stream. Assuming you print this year, it seems you insert it as a char with value 13 which happens to be \r. The upshot of this is that your later output will just overwrite the output as it will be on the same line. You can verify this by explicitly inserting a newline (\n) before flushing the stream.

like image 69
Dietmar Kühl Avatar answered Oct 21 '22 11:10

Dietmar Kühl