显示进度指示器

发布于 2024-11-18 18:42:15 字数 106 浏览 5 评论 0原文

我在 Linux 中使用 C。如何显示进度指示器,让我知道程序(或程序的一部分)何时完成?例如,它可能类似于“正在搜索...67%”,并且百分比将不断增加,直到搜索部分结束。

谢谢。

I'm using C in Linux. How do I show a progress indicator that will let me know when the program (or parts of the program) will complete? For example, it could be something like "Searching...67%" and the percentage will keep increasing until the Searching portion ends.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

っ〆星空下的拥抱 2024-11-25 18:42:15

'\r' 字符写入 stdout 以使光标返回到行的开头,以便您可以覆盖该行。例如:

for (i=0; i<100; i++) {
    printf("\rSearching...%d%%", i);
    fflush(stdout);
    sleep(1);
}

Write a '\r' character to stdout to return the cursor to the beginning of the line so you can overwrite the line. For example:

for (i=0; i<100; i++) {
    printf("\rSearching...%d%%", i);
    fflush(stdout);
    sleep(1);
}
许久 2024-11-25 18:42:15

我相信如果您执行以下操作:

while (perc < 100) {
    printf("Searching... %d%%\r", perc); 
    fflush(stdout);
    //do work
}

fflush() 对于避免行缓冲是必要的。请注意,我使用的是 \r 而不是 \n

I believe if you do something like:

while (perc < 100) {
    printf("Searching... %d%%\r", perc); 
    fflush(stdout);
    //do work
}

the fflush() is necessary to avoid the line buffering. Note that I am using \r and not \n.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文