difference in "flush", "ofstream::flush", and "endl"

Hi, I am trying to understand the point and use of flushing the buffer.
When testing the code in the examples in the cplusplus.com reference for ofstream::flush, flush, and "endl" they all produce the exact same output as

cout >> n; does by itself in a for loop.

I don't understand why I would use one of the other options. Can someone give me an example that doesn't produce the exact same output so I can see the difference?

Thanks
i dont really know flush but I think endl is a shorthand of "\n"

try this:
cout << endl << n << endl;

is different with

cout << n << endl;

and different with

cout << endl << n;


cause like i said you're appending "\n" to cursor

BTW, I think you can't never compile cout >> n ^_^
Last edited on
I believe, that, whereas "endl" inserts a newline into a stream and then flushes the stream buffer, "flush" does just what it says - flushes the stream.

Oh, and what is "cout>>n" supposed to mean? :P
Flushing the buffer is, in this case forcing the contents of anything in cout to the screen. For example, if your program was hanging and to try and find the problem you did

1
2
cout << "Making blocking call";  // without an endl
blockingcall();


and the blocking call is hanging, there are circumstances where you would never see the cout until the program dies. Add in the endl and you are guaranteed the cout goes to the screen before the blocking call.

Topic archived. No new replies allowed.