7

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?

Tyler
  • 827
  • 2
  • 11
  • 25
  • 4
    I spy a [rather big X/Y problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). How are you deciding that flush didn't work? – sehe Oct 20 '13 at 23:09
  • As to the Y part of the XY problem, look here [How to parse and validate a date in std::string in C++?](http://stackoverflow.com/questions/19482378/how-to-parse-and-validate-a-date-in-stdstring-in-c), e.g. – sehe Oct 20 '13 at 23:14
  • Try to make an SSCCE (http://meta.stackexchange.com/questions/22754/sscce-how-to-provide-examples-for-programming-questions). This probably will help you solve your problem by yourself. – Amadeus Oct 20 '13 at 23:15

3 Answers3

10

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.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • Erm. Doesn't `\n` cause stdout to flush anyways on most systems? – sehe Oct 20 '13 at 23:11
  • 2
    @sehe: sadly, it will probably do so because most IOStreams implementation choose to be slow and delegate to `` routines. Since most programs also don't call `std::ios_base::sync_with_stdio(false)` (which they should if they don't mix `` with `` operations on the standard streams), it will write each character to `stdout`. However, an implementation can do better in which case it may not flush on `\n`. – Dietmar Kühl Oct 20 '13 at 23:14
  • Thankyou, for your answer. I took the `substr` of my date from 0 to (length-1) and it removed the carriage return. I also did a `find` to verify it was a `'\r'` character in there and it was the case. – Tyler Oct 20 '13 at 23:36
6

There's a system buffer on cout that will still buffer your output in modern Linux systems.

To disable it for a run of your program use the command stdbuf like this:

stdbuf -o 0 ./yourprogram --yourparams

If you need to disable the buffering in the debugger, use it like this:

stdbuf -o 0 gdb --args ./yourprogram --yourparams
Nicolay77
  • 2,085
  • 25
  • 20
1

Flush only writes the buffers for the stream to the actual device (file or tty).

It doesn't move to the next line, if that's what you expect.

This is by design.

Note, here's a cleaner version of the code, that appears to work as advertised (with or without the flush, for that matter):

See it Live on Coliru

#include <sstream>
#include <iostream>
#include <vector>
#include <cassert>

struct Game { 
    std::string _date;
    std::string date() const { return _date; }
};

int main()
{
    for (Game game : std::vector<Game> { {"01/02/1999"}, {"24/10/2013"}})
    {
        std::istringstream stm (game.date());
        int day, month, year;
        char delim = '/';

        std::cout << "Date before: " << game.date();

        stm >> month >> delim;
        assert(stm && '/' == delim);

        stm >> day >> delim;
        assert(stm && '/' == delim);

        stm >> year;

        std::cout << " Date after: " << " | " << month << "/" << day << "/" << year << std::endl;
    }
}
sehe
  • 374,641
  • 47
  • 450
  • 633