Flushing the standard output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jamik
    New Member
    • Aug 2009
    • 7

    Flushing the standard output

    What is the meaning of "flushing the standard output"?
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    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") ;

    Comment

    • jamik
      New Member
      • Aug 2009
      • 7

      #3
      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

      • mac11
        Contributor
        • Apr 2007
        • 256

        #4
        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

        • jamik
          New Member
          • Aug 2009
          • 7

          #5
          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

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Why are you waiting for two key presses at the end of your program?

            Comment

            • jamik
              New Member
              • Aug 2009
              • 7

              #7
              It is not changing anything....

              Comment

              • mac11
                Contributor
                • Apr 2007
                • 256

                #8
                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

                • jamik
                  New Member
                  • Aug 2009
                  • 7

                  #9
                  Just that is the question, while I compile it's working correct!!!!

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by jamik
                    Just that is the question, while I compile it's working correct!!!!
                    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,

                    Jos

                    Comment

                    • jamik
                      New Member
                      • Aug 2009
                      • 7

                      #11
                      I solved that problem already, now there is a problem, some tests Wrong Answer...

                      Comment

                      • mac11
                        Contributor
                        • Apr 2007
                        • 256

                        #12
                        Can you show us the input you give and what output you get - and what output you should get?

                        Comment

                        • jamik
                          New Member
                          • Aug 2009
                          • 7

                          #13
                          I don't know about inputs, becaus I wrote that task cheks the testing system,not me....

                          Comment

                          Working...