动态界面,不需要刷新 ,使用printf 函数解决问题 printf(“\b“) 清除上一个 输出。

 解释是无效的,直接看代码!

代码:

/************************************************************************************************************************************************************************************************************************
 *文件名:
 *作  者:She001
 *时  间:
 *版  本:
 *作  用:
****************************************************************************************************************************************************************************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<sys/wait.h>
#include<a.out.h>
#include<signal.h>
#include<stdarg.h>
#include<sys/socket.h>
#include<utime.h>
#include<sys/utsname.h>
#include<sys/times.h>
#include<sys/un.h>
#include<ctype.h>
#include<dirent.h>
#include<sys/time.h>
#include<sys/resource.h>
#include<syslog.h>
#include<pthread.h>
#include<semaphore.h>
#include<sys/time.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<sys/msg.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<sqlite3.h>
#include<sys/select.h>
#include<sys/poll.h>
void progress_bar() //进度条
{
// ****************************** 配置 ***************************
    // 最后100%时的输出形式
    const char *LastStr = "[--------------------] 100%";

    // 进度条标志,可以改用"*"或其它符号
    const char Progressicon = '-';

    // 进度每加5,进度条就会加一格,注意Step值要和LastStr中的"-"数量相匹配,两者相乘等于100
    const int Step = 5;

    // 总共需要多少进度标志,即LastStr中"-"的数量
    const int iconMaxNum = 20;

    // 每隔100ms打印一次,这里同时每隔200ms会让进度加1
	printf("正在进入服务器:");
    const int Printinterval = 20000;
    // ****************************************************************

    for (int i = 0; i <= 100; ++i)
    {
        // -------------- 打印进度条 --------------
        printf("[");
        int currentindex = i / Step;
        for (int j = 0; j < iconMaxNum; ++j)
        {
            if (j < currentindex)
            {
                printf("%c", Progressicon); // 打印进度条标志
            }
            else
            {
                printf(" "); // 未达进度则打印空格
            }
        }

        printf("] ");
        // ----------------------------------------

        // -------- 打印数字进度 ----------
        printf("%3d%%", i);
        fflush(stdout);//关键在这里 ,这里是行缓冲的一次性的输出 
        // -------------------------------

        usleep(Printinterval);

        for (int j = 0; j < strlen(LastStr); ++j)
        {
            printf("\b"); // 回删字符,让数字和进度条原地变化  "\b"控制光标前移一个字符
        }
        fflush(stdout);//关键在这里 ,这里是行缓冲的一次性的输出 他一次性的回退 n 格,刚好把之前输出的全部清除
    }
    printf("\n");
}

int main(int argc,char *argv[])
{

	progress_bar();

       return 0;

}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值