如何更新命令行输出? [英] How to update command line output?

查看:98
本文介绍了如何更新命令行输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux中, stdout 到命令行,我想更新特定区域,例如 apt-get 输出:

  54%[等待头文件] [等待头文件] 211 kB / s 3s 



kB / s



如果我们使用 printf ,那么我们将得到多行输出。我试过下面的 printf

  printf(\e [ 1; 1H \e [2J); 

但它会清除所有输出。



解决方案

使用回车。它会将光标移回到行的开头,从中可以覆盖之前的行。例如:

  printf(Hello,world!\rX); 

将显示为:

  Xello,world! 

请务必经常刷新 stdout 希望它可见:

  fflush(stdout); 


In Linux, While stdout to command line, I want to update specific area, such as the apt-get output:

54% [Waiting for headers] [Waiting for headers]        211 kB/s 3s

the percentage, kB/s and second will update each second.

If we use printf then we will get multiple lines output them. I have tried following printf:

printf("\e[1;1H\e[2J");

But it cleans all the output.

My question is how to update specific area and keep others stable?

解决方案

Use the carriage return. It will move the cursor back to the start of the line, from which you can overwrite what was in the line before. For example:

printf("Hello, world!\rX");

Will be visible as:

Xello, world!

Make sure you flush stdout often if you want it to be visible:

fflush(stdout);

这篇关于如何更新命令行输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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