Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a output field in terminal without output a new line? [duplicate]

Tags:

Possible Duplicate:
How to update a printed message in terminal without reprinting (Linux)

I have c++ code, performing some simulations.

I want to show the percentage of my simulation, but I don't want to output a new line every step, like

%1

%2

%3

...

Is there a way, in c++ or in shell scripts to show the progress without creating new lines?

Thanks

Edit 1

Anyone know how to update a number on my personal webpage without refreshing the whole page?

Thanks

Edit 2

double N=0;
forAll (internalIDs_, i) {
    N++;
    double percent = 100*N/internalIDs_.size();
    // Info<< "\rProgress: " << percent << "%" << endl;
    printf("\r[%6.4f%%]",percent);}

The terminal cursor keeps blinking cyclically through the numbers, very annoything, how to get rid of this?

like image 212
Daniel Avatar asked Jun 11 '12 15:06

Daniel


2 Answers

The trick used for this is to return to the first position in the current line instead of progressing to the next line.

This is done by writing the \r character (carriage return) to the terminal/stdout.

like image 113
ypnos Avatar answered Sep 22 '22 18:09

ypnos


cout << "\r%1";
cout << "\r%2";
cout << "\r%3";

...

\r - move at the begin of line;

but! if :

cout << "\rsomelongmessage";
cout << "\rshort";

then you get at out:

shortongmessage

because of:

somelongmessage
^^^^^
short

but you can:

cout << "\rsomelongmessage";
cout << "\rshort          ";

then you get finally:

short
like image 38
ScaV Avatar answered Sep 24 '22 18:09

ScaV