how to print on screen in realtime

Discussion in 'C++' started by zl2k, Jul 13, 2006.

  1. zl2k

    zl2k Guest

    hi, all
    I am trying to moniter the runing of a program by printing out some
    characters in a line at each stage. say,

    //program part1
    cout<<"part1 ";
    //program part2
    cout<<part2 ";
    ....

    I was expecting the program will pring "part1 part2 part3 part4 ...."
    in realtime but instead it only print out when the program is finished.
    I force it to print out by adding <<endl for each cout then the output
    turns to vertical which is hard to see. My question is, how may I print
    it in a line in realtime? Thanks.

    zl2k
     
    zl2k, Jul 13, 2006
    #1
    1. Advertisements

  2. Read about 'flush' manipulator. If it doesn't work, use your platform's
    capabilities instead.

    V
     
    Victor Bazarov, Jul 13, 2006
    #2
    1. Advertisements

  3. zl2k

    Adi Guest

    cout << flush;
    cout.flush();
    cout << endl;

    This will flush the buffered output to be printed. The output is
    buffered and will be printed when it reaches a limit or when the
    program ends or some other condition like that.

    Adi
     
    Adi, Jul 13, 2006
    #3
  4. zl2k

    Marcus Kwok Guest

    I don't know about "realtime", but you can try adding std::flush instead
    of std::endl.

    // program part1
    cout << "part1 " << flush;
    // program part2
    cout << "part2 " << flush;
     
    Marcus Kwok, Jul 13, 2006
    #4
  5. As Victor Bazarov rightly pointed out, you can use flush at each point
    where you want to force output, like so:

    //program part1
    cout << "part1 " << flush;
    //program part2
    cout << "part2 " << flush;

    Another thing you can use is the "endl" manipulator, which first
    prints an endline character ('\n'), then flushes the stream.
    I tend to write text a line at a time in my progrms with immediate
    output, like so:


    //program part1
    cout << "part1" << endl;
    //program part2
    cout << "part2" << endl;

    which prints:

    part1
    part2
    part3
    etc.

    in real-time.

    I just thought I'd mention that option.


    --
    Cheers,
    Robbie Hatley
    East Tustin, CA, USA
    lone wolf intj at pac bell dot net
    (put "[usenet]" in subject to bypass spam filter)
    http://home.pacbell.net/earnur/
     
    Robbie Hatley, Jul 14, 2006
    #5
  6. zl2k

    Marcus Kwok Guest

    The OP had already considered that option but found the output to be too
    hard to see.
     
    Marcus Kwok, Jul 14, 2006
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.