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. Advertising

  2. zl2k wrote:
    > 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.


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

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 13, 2006
    #2
    1. Advertising

  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

    zl2k wrote:
    > 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
    Adi, Jul 13, 2006
    #3
  4. zl2k

    Marcus Kwok Guest

    zl2k <> wrote:
    > 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.


    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
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Jul 13, 2006
    #4
  5. "zl2k" <> wrote:

    > 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.


    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

    Robbie Hatley <> wrote:
    > "zl2k" <> wrote:
    >> I force it to print out by adding <<endl for each cout then the output
    >> turns to vertical which is hard to see.

    >
    > 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.


    The OP had already considered that option but found the output to be too
    hard to see.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Jul 14, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henrik H
    Replies:
    1
    Views:
    636
    Scott Allen
    Oct 25, 2004
  2. Bryan Shaw

    Lock Print Screen and File Print

    Bryan Shaw, Apr 19, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,933
    Kevin Spencer
    Apr 20, 2005
  3. keto
    Replies:
    0
    Views:
    897
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    335
    David Cournapeau
    Dec 30, 2008
  5. Replies:
    0
    Views:
    253
Loading...

Share This Page