c 语言进度条,获取终端大小

12 篇文章 0 订阅
////////////////////////进度条显示//////////////////////////////////

/*
#include <stdio.h>
#include <string.h>

void displayProgress(int progress){
        int k = 0;
        for (k=0; k<106; k++)
                putchar('\b');//将当前行全部清空,用以显示最新的进度条状态
        int j = 0;
        for (j=0; j<progress; j++)
                putchar('+');//打印进度条上已经完成的部分,用‘+’表示
        for (j=1; j<=100-progress; j++)
                putchar('-');//打印进度条上还有多少没有完成的
        fprintf(stdout, "  %3d%%",progress);
        fflush(stdout);
}


int main()
{
    int i;
    for(i=0;i<100;i++){
        displayProgress(i);
        sleep(1);
    }


}
 */

/* #include<stdio.h>
#define N 20
void mBar(float fBar){

    int i,j,k,m;
    for ( i=0;i<N+6;i++ ) printf("\b");
    k=N*fBar; m=fBar*100; printf("[");
    for ( i=0;i<k;i++ ) printf("=");
    for ( j=0;j<N-k;j++ ) printf(" ");
    printf("]%3d%%",m); 
}
void delay(int n) { int i,j,k; for ( i=0;i<n;i++ ) for ( j=0;j<0x07fff;j++ ) k++; }
void main()
{
    float x;
    int i;
    for ( i=0,x=0;i<100;i++,x+=0.01 ) { mBar(x); delay(1024); }
    mBar(1);
} */


/* #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 //类似wget的进度条的实现,实际就是转移符\r的使用,\r的作用是返回至行首而不换行
 int main(int argc, char *argv[])
 {
     unsigned len = 60;
     char *bar = (char *)malloc(sizeof(char) * (len + 1));
     int i = 0;
     for (i = 0; i < len + 1; ++i)
     {
         bar[i] = '#';
     }
     for (i = 0; i < len; ++i)
     {
         printf("progress:[%s]%d%%\r", bar+len-i, i+1);
         fflush(stdout);//一定要fflush,否则不会会因为缓冲无法定时输出。
         usleep(100000);
         //sleep(1);
     }
     printf("\n");
     return 0;
 } 
 */
 ////////////////////////获取终端大小//////////////////////////////////


#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <sys/ioctl.h> 
int main(void) 
{
    struct winsize size;  
    if (isatty(STDOUT_FILENO) == 0)  
        exit(1);  
    if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &size)<0) 
    {
        perror("ioctl TIOCGWINSZ error");
        exit(1);
    } 

    printf("%d rows, %d columnsn", size.ws_row, size.ws_col);  
    return 0; 
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值