Code: Select all
...
cout.flush();
...
Moderator: Board moderators
Code: Select all
...
cout.flush();
...
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;
}
Code: Select all
#include <iostream>
using namespace std;
int main(){
cout << "a" << flush;
while(1){}
}
Confused...Krzysztof Duleba wrote:But now it does make a difference, doesn't it?Code: Select all
#include <iostream> using namespace std; int main(){ cout << "a" << flush; while(1){} }
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).
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?Roby wrote:[...]Confused...it's just looping forever, isn't it? or anything else? I really don't understand...
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.
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;
}
Thanx moha... now I understand... thanx a lot...jtmh wrote:Maybe, with files, it's easier to understand: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.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; }