Ada*_*eld 10
你可以通过打印退格字符'\b'和原始回车'\r'(没有换行符)来做一些简单的事情.退格键将光标向后移动一个字符,允许您覆盖输出,回车符将光标移回当前行的开头,允许您覆盖当前行.
这是一个进度条的简单示例:
int progress = 0;
while(progress < 100)
{
// Note the carriage return at the start of the string and the lack of a
// newline
printf("\rProgress: %d%%", progress);
fflush(stdout);
// Do some work, and compute the new progress (0-100)
progress = do_some_work();
}
printf("\nDone\n");
Run Code Online (Sandbox Code Playgroud)
请注意,只有在写入实际终端时才应该这样做(而不是重定向到文件或管道).你可以测试一下if(isatty(fileno(stdout)) { ... }.当然,如果您使用任何其他库(如curses或ncurses),情况也是如此.
| 归档时间: |
|
| 查看次数: |
2031 次 |
| 最近记录: |