将光标移动到当前行的开头 [英] Moving the cursor to the beginning of the current line

查看:83
本文介绍了将光标移动到当前行的开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在同一位置打印当前时间(通过使用printf),但是我想在无限循环中执行它,例如:

I want to print current time (by using printf) in same place, but i want to do it in infinite loop eg:

while(1) {printf("Date and Time are %s", asctime(localtime(&current))); } 

.因此,在使用printf之前,我应将光标向后移至其凝视位置.怎么做?

. So before i use printf i should move cursor backward to its staring position. How to do it ?

提前谢谢

推荐答案

为简单地将光标移动到当前行的开头,您可以打印"\ r",此操作就可以完成.请注意,它不会删除旧文本,因此请小心将其覆盖或使用ANSI代码清除.

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.

在使用ANSI/VT控制代码的系统上,可以打印"\ 033 [1; 2H""来定位光标.它将移动光标,并且不会在屏幕上打印任何内容.值1和2是行和列,因此请将其更改为使用不同的位置.

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.

对于颜色和其他内容,还有其他代码: http://bluesock.org/〜willg/dev/ansi.html

There are also other codes for colors and other things: http://bluesock.org/~willg/dev/ansi.html

请注意,这些代码都不是可移植的,并且它们可能无法在所有系统上运行(最值得注意的是,默认情况下,它们在某些Microsoft系统上无法运行).不支持的系统会在屏幕上(代码本身)显示一些垃圾.

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

这篇关于将光标移动到当前行的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆