flush

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
Roby
Experienced poster
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:

flush

Post by Roby »

I just don't understand, what flush() means in C++? I mean flush with cout object, like this:

Code: Select all

...
cout.flush();
...
Is there also for cin object? Thanx...

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

Google for it. It takes less than writing a post.

Roby
Experienced poster
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:

Post 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...

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post 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).

Roby
Experienced poster
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:

Post 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...

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post 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.

jtmh
New poster
Posts: 16
Joined: Sat Jul 15, 2006 8:34 pm
Location: Taiwan

Post 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. :)

Moha
Experienced poster
Posts: 216
Joined: Tue Aug 31, 2004 1:02 am
Location: Tehran
Contact:

Post 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.

Roby
Experienced poster
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:

Post 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... :wink:

Post Reply

Return to “C++”