Page 1 of 1
flush
Posted: Tue Jul 18, 2006 2:51 am
by Roby
I just don't understand, what flush() means in C++? I mean flush with cout object, like this:
Is there also for cin object? Thanx...
Posted: Tue Jul 18, 2006 3:39 am
by Krzysztof Duleba
Google for it. It takes less than writing a post.
Posted: Tue Jul 18, 2006 3:58 am
by Roby
I still don't get it... from
http://www.cs.hmc.edu/~geoff/classes/hm ... es/io.html and
http://www.gillius.org/ctut/app_b.htm it explain that flush usage is to output the buffer... but, when I try with these code:
Code: Select all
#include <iostream.h>
int main()
{
char mystrin[10] = "HELLO";
cout << "My string is " << mystring << " plus a null character\n" << flush;
return 0;
}
I didn't catch something different if I removed that flush manipulator. Can't someone explain to me please? Thanx in advance...
Posted: Tue Jul 18, 2006 5:30 am
by Krzysztof Duleba
Code: Select all
#include <iostream>
using namespace std;
int main(){
cout << "a" << flush;
while(1){}
}
But now it does make a difference, doesn't it?
You should use flush only when you know what you're doing, in very few and specific situations (for instance when you're dealing with pipes).
Posted: Tue Jul 18, 2006 5:59 am
by Roby
Krzysztof Duleba wrote:Code: Select all
#include <iostream>
using namespace std;
int main(){
cout << "a" << flush;
while(1){}
}
But now it does make a difference, doesn't it?
You should use flush only when you know what you're doing, in very few and specific situations (for instance when you're dealing with pipes).
Confused...

it's just looping forever, isn't it? or anything else? I really don't understand...
Posted: Tue Jul 18, 2006 10:25 am
by sumankar
Roby wrote:[...]Confused...

it's just looping forever, isn't it? or anything else? I really don't understand...
Don't miss the wood for the trees. The loop is not important in itself. Take out the `flush' from the cout statement and then try to run the program. Can you see any difference?
Here's the relevant section from one of the websites you've posted:
Code: Select all
Forcing all buffered output to actually be printed is known as "flushing" the stream. A flush can be forced by calling the flush function associated with each output stream, inserting the magic variable flush into the stream, or inserting endl.
Note: you can flush only
output streams. Doing that on an input stream is undefined behaviour.
Posted: Tue Jul 18, 2006 10:43 am
by jtmh
Maybe, with files, it's easier to understand:
Code: Select all
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile("__test__.txt");
outfile << "You will not see this string till outfile.flush() is run.";
cin.get(); // open __test__.txt to check if it contains the string above
// and then press <Enter> in the console window to continue
outfile.flush();
cin.get(); // RE-open __test__.txt to check it out again
// and then press <Enter> in the console window to exit
return 0;
}
Run the compiled program and follow the instructions in the comments, and you should be able to see the effect of flush(). Hope this could help.

Posted: Tue Jul 18, 2006 10:48 pm
by Moha
May be the best effect of flush can be demonestrated by using cerr and cout together. cerr is unbuffer stream but cout is a buffer one.
Posted: Fri Jul 21, 2006 2:46 am
by Roby
jtmh wrote:Maybe, with files, it's easier to understand:
Code: Select all
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile("__test__.txt");
outfile << "You will not see this string till outfile.flush() is run.";
cin.get(); // open __test__.txt to check if it contains the string above
// and then press <Enter> in the console window to continue
outfile.flush();
cin.get(); // RE-open __test__.txt to check it out again
// and then press <Enter> in the console window to exit
return 0;
}
Run the compiled program and follow the instructions in the comments, and you should be able to see the effect of flush(). Hope this could help.

Thanx moha... now I understand... thanx a lot...
