printf固定一行打印倒计时的实现

主要是依赖函数:printf("\b") 退格格式符

fflush(stdout),刷新标准输出缓冲区,把输出缓冲区里的东西打印到标准输出设备上

示例:

#include<stdlib.h>
#include <stdio.h>
#include <time.h>
#include <windows.h>

int main()
{
    int i;

#if 1
    int bootdelay = 3;
    printf("Hit any key to stop autoboot: %2ds ", bootdelay);    //"%2ds " 2byte数字 1字节's' 1字节空格
    fflush(stdout);
    while (bootdelay > 0) {
        Sleep(1000);
        --bootdelay;
        printf("\b\b\b\b%2ds ", bootdelay);    //因格式占4byte,2byte数字、1字节‘s’、1字节空格,所以需要4个'\b' backspace 退格非删除
        fflush(stdout);
    }
#endif
    printf("\n");

    printf(".................\n");
    for (i = 5; i > -1; i--) {
        if (i == 5) {
            printf("Wait %ds", i);
            fflush(stdout);
            Sleep(1000);
        } else {
            printf("\b\b%ds", i);
            fflush(stdout);
            Sleep(1000);
        }
    }
    printf("\n");
    return 0;
}

posted @ 2019-07-15 13:49  壹点灵异  阅读(756)  评论(0编辑  收藏  举报