What is the meaning of "flushing the standard output"?
Flushing the standard output
Collapse
X
-
Standard output is buffered, flushing it means to empty the buffer. Your output statements are held in memory until the buffer fills or you flush it, then they are dumped to the console all at once. The reason for this is time savings.
For instance, if you have a program that does
cout << "something" ;
the "something" will not make it to the screen until somebody flushes the buffer or it fills up, which may be quite some time but usually nobody cares.
The buffer is flushed by using 'flush' or 'endl' in your cout statement.
I'm assuming c++, but if you're referring to c land the same concept holds true except that you printf a newline "\n" to flush the buffer.
Usually, if you want your output to appear immediately (unbuffered) you write to stderr using
cerr << "something" ;
or
fprintf(stderr, "whatever") ; -
Thanks a lot... Just I understood this one... I asked to write a program using fflush(), I wrote but testing system does not accept. this is taskhttp://day0.ioi2009.org/data/Hill.pdf and here is my solution:
Code:#include<iostream> #include<cstdio> using namespace std; main (){ int i,j,n,m,maxi,maxj,max=0,a; scanf ("%i %i",&n,&m); for (i=1; i<=m; i++){ for (j=1; j<=n; j++){ scanf ("%i",&a); printf ("0 %i %i\n",j,i); fflush(stdout); if (a>max){ max=a; maxi=i; maxj=j; } if (i==m&&j==n-1){break;} } } printf ("1 %i %i\n",maxj,maxi); fflush(stdout); getchar(); getchar(); return 0; }Comment
-
I looked at your assignment page and it gives an expected input and output sample.
I think you just need to run your program and look closely at your code and follow the path of execution and you should be able to figure out the problem.
Have you tried running your program using the given sample input?
What did it not do correctly?Comment
-
I cannot understand, while I compile everything is OK, but when I send it to testing system it does not accept... It says that Time Limit Exceeded... Can you just see this testing system... Try to send the solution, maybe you can understand... Please help me in this condition...Comment
-
I don't want to just tell you what's wrong with your program.
Spend a little time and actually run it on your own computer not send it to some automated tester. Type in the sample input your assignment sheet provides. Hint Hint!! Look at what your program gives for output versus what the sample output should be when you give it the first two lines.
Much of becoming a programmer is learning how to solve faulty logic and dissect problems.Comment
-
Please read reply #6 again: your program can't end without human intervention (s/he has to press two character). Possibly the test engine waits ad nauseam and concludes that your program hangs. Take out those two getchar() calls.
kind regards,
JosComment
Comment