I want to print current time (by using printf) in same place, but i want to do it in infinite loop eg:
我想在同一个地方打印当前时间(通过使用printf),但我想在无限循环中执行它,例如:
while(1) {printf("Date and Time are %s", asctime(localtime(¤t))); }
. So before i use printf i should move cursor backward to its staring position. How to do it ?
。所以在我使用printf之前,我应该将光标向后移动到它的凝视位置。怎么做 ?
thx in advance
提前thx
4 个解决方案
#1
7
For simply moving the cursor to the beginning of the current line, you may print "\r", which does just that. Notice that it does not erase the old text, so be careful to either overwrite it or to clear with an ANSI code.
只需将光标移动到当前行的开头,就可以打印“\ r \ n”,就可以了。请注意,它不会删除旧文本,因此请小心覆盖它或使用ANSI代码清除。
On systems using ANSI/VT control codes, you can print "\033[1;2H" to position the cursor. It will move the cursor and will not print anything on screen. The values 1 and 2 are the row and the column, so change them to use different positions.
在使用ANSI / VT控制代码的系统上,您可以打印“\ 033 [1; 2H”来定位光标。它将移动光标,不会在屏幕上打印任何内容。值1和2是行和列,因此将它们更改为使用不同的位置。
There are also other codes for colors and other things: http://bluesock.org/~willg/dev/ansi.html
还有其他颜色和其他代码:http://bluesock.org/~willg/dev/ansi.html
Notice that none of these codes are portable and they may not work on all systems (most notably they don't work by default on some Microsoft systems). Non-supporting systems will instead display some garbage on screen (the code itself).
请注意,这些代码都不是可移植的,并且它们可能无法在所有系统上运行(最值得注意的是,它们在某些Microsoft系统上默认不起作用)。非支持系统将在屏幕上显示一些垃圾(代码本身)。
#2
6
write a \r
写一个\ r
while(1) {
printf("\rDate and Time are %s ", asctime(localtime(¤t)) );
fflush(stdout);
}
#3
3
You could do it like this (look at the \r with many spaces!):
你可以这样做(看看有很多空格的\ r \ n):
while(1) {
printf("Date and Time are %s\r ", asctime(localtime(¤t)));
fflush(stdout);
}
#4
0
It might work to print a "\r" at the start of the line.
它可能适用于在行的开头打印“\ r”。
#1
7
For simply moving the cursor to the beginning of the current line, you may print "\r", which does just that. Notice that it does not erase the old text, so be careful to either overwrite it or to clear with an ANSI code.
只需将光标移动到当前行的开头,就可以打印“\ r \ n”,就可以了。请注意,它不会删除旧文本,因此请小心覆盖它或使用ANSI代码清除。
On systems using ANSI/VT control codes, you can print "\033[1;2H" to position the cursor. It will move the cursor and will not print anything on screen. The values 1 and 2 are the row and the column, so change them to use different positions.
在使用ANSI / VT控制代码的系统上,您可以打印“\ 033 [1; 2H”来定位光标。它将移动光标,不会在屏幕上打印任何内容。值1和2是行和列,因此将它们更改为使用不同的位置。
There are also other codes for colors and other things: http://bluesock.org/~willg/dev/ansi.html
还有其他颜色和其他代码:http://bluesock.org/~willg/dev/ansi.html
Notice that none of these codes are portable and they may not work on all systems (most notably they don't work by default on some Microsoft systems). Non-supporting systems will instead display some garbage on screen (the code itself).
请注意,这些代码都不是可移植的,并且它们可能无法在所有系统上运行(最值得注意的是,它们在某些Microsoft系统上默认不起作用)。非支持系统将在屏幕上显示一些垃圾(代码本身)。
#2
6
write a \r
写一个\ r
while(1) {
printf("\rDate and Time are %s ", asctime(localtime(¤t)) );
fflush(stdout);
}
#3
3
You could do it like this (look at the \r with many spaces!):
你可以这样做(看看有很多空格的\ r \ n):
while(1) {
printf("Date and Time are %s\r ", asctime(localtime(¤t)));
fflush(stdout);
}
#4
0
It might work to print a "\r" at the start of the line.
它可能适用于在行的开头打印“\ r”。