'cout' statement from within a 'for' loop and 'if' statement

Hi Rooters

I have an analysis class which is based on the TTree::MakeClass skeleton.

If I insert a ‘cout’ statement within the ‘for’ loop of the Loop() method, then that
output appears on the screen for every event, i.e. every time that the ‘for’ loop
repeats, as expected.

However, if I replace the cout statement with a very simple progress indicator:

  if (jentry==counter*nentries/100) {
    cout << counter << "% ";

then the output of the cout statements is not immediate. Sometimes, if all goes
well, then they all arrive together at the end of the loop. On other occasions,
if the method doesn’t terminate smoothly, they don’t appear at all.

Can anyone answer the following questions:
What is causing this and how can I correct it? Is there a better way to get an
indication of the progress during a long job?


Red Hat Linux release 8.0 (Psyche)
Kernel 2.4.18-17.8.0 on an i686

Root Version 3.05/06


There are 2 main output in C/C++: The regular output (stdout, cout) and the error output (stderr, cerr). The difference between the 2 is that the regular output is buffered while the error output is not buffered.

In particular, this means that the system is allowed to print the standard output whenever it feels it has time. In case of crashes this also means that some output can be lost.

On the other hand the error output has to be printed immediately. This is the output to use when you want to be clearly informed on the order and timing of things happening.


I’ll just add that you can use cout.flush() to flush the buffer whenever needed.