Linux Linux第一个小程序——进度条

1.补充概念

1.1回车换行:回车换行是两个概念;1.换行是将光标从第一行挪到第二行;2.回车是将光标挪到第二行的最左面;C语言是将回车换行一起用,他是可以分开用的;

1.2缓冲区

// 现象第一组代码,先休眠后打印;第二组代码先打印后休眠
#include<unistd.h>//第一组
 int main()
{
printf("hello Linux!");    //1

sleep(2);                  //2

return 0;
}
/
#include<unistd.h>//第二组
 int main()
{
printf("hello Linux!\n");//带\n

sleep(2);

return 0;
}

关于此现象说法是不正确的;因为c语言是顺序执行的,所以执行顺序一定是先执行1 后执行2;

1.3那么在我sleep期间,hello Linux在哪?

答:因为被打印了了出来,数据没有被丢失,一定被保存了起来——缓冲区(c语言维护的一段内存)——缓冲区是存在的

//  先打印hello……再休眠2秒后打印bash提示符;
#include<unistd.h>//第一组
 int main()
{
printf("hello Linux!");    //1
fflush(stdout);           //消息存在于文件流stdout中,刷新stdout流
sleep(2);                  //2

return 0;
}

2整套代码(因为篇幅的缘故,就不进行.h、.c的操作了)

2.1注:vs下Sleep函数包含在<windows.h>下,S必须大写,1000毫秒为1秒

2.2注:linux下sleep函数,需要引入头文件unistd.h ,单位1秒

            linux下usleep函数,需要引入头文件unistd.h ,单位微秒 ,1秒=1000毫秒  1毫秒=1000微秒

pragma once    //Linux 下的代码
#include<string.h>
#include<unistd.h>
#include<stdio.h>

#define num 101
#define style '#'
const char* lable = "|/-\\";

void test.h()
{
    char bar[num];
    memset(bar,'\0',sizeof(bar));
    int cnt=0;
    int len = strlen(lable);
    while(cnt <=100)
    {
        //printf("%s\r",bar);//没有\n,就没有立即刷新,因为下显示器模式是行刷新;
        //printf("[%s]\r",bar); //打印出来括号是跟着走的
        printf("[%-100s][%d%%][%c]\r", bar, cnt,lable[cnt%len]);
                                 //预留100个空间,且从左向右打印
                                 //一个%属于格式化控制的特殊字符,%%代表百分号

        fflush(stdout);
        bar[cnt++]=style;
        //sleep(1);     //时间太长
        usleep(100000)
    }

}

int main()
{
    test();
    return 0;
}

 二次修改

#pragma once   //vs下的代码;
#include<string.h>
#include<iostream>
#include<stdio.h>
#include<Windows.h>
using namespace std;

#define num 100
#define style '#'
const char* lable = "|/-\\";

void test()
{
    char bar[num];
    memset(bar, '\0', sizeof(bar));
    int cnt = 0;
    int len = strlen(lable);
    while (cnt <100)
    {
        printf("[%-100s][%d%%][%c]\r", bar, cnt,lable[cnt%len]);

        //fflush(stdout);     //c语言  fflush用法
        //cout << bar << "\r" << flush;  //c++ flush写法
        bar[cnt++] = style;
        Sleep(100);
    }

}

int main()
{
    test();
    return 0;
}

 三次修改,当有下载任务时,变化百分比

#pragma once
#include<string.h>
#include<iostream>
#include<stdio.h>
#include<Windows.h>
using namespace std;

#define num 102
#define style '#'
const char* lable = "|/-\\";
char bar[num];

void test(int rate)
{
    if(
    if (rate < 0 || rate >100)
        return;
    int len = strlen(lable);
    printf("[%-100s][%d%%][%c]\r", bar, rate,lable[rate%len]);    
    fflush(stdout);
    bar[rate++] = style;
    if(rate==100) init();
}
void init()//初始化
{
    memset(bar,'\0',sizeof(bar));
}

int main()
{
    //一共1G的下载,已下载100MB
    int total = 1000;
    int cur = 0;
    while (cur<=total)
    {  
        int i = cur * 100 / total;
        test(i);//你告诉我一个百分比,我给你打一次进度条   
        cur += 10; //因为c语言字符串是以\0为判断,是否打印结束,所以加10,若加100,程序就垮了
                   //达不到预想的水平;
        Sleep(100);
    }
    
    return 0;
}

  • 13
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值