1. 程式人生 > >C語言printf控制游標位置和清空螢幕

C語言printf控制游標位置和清空螢幕

控制游標位置:

void locateCursor(const int row, const int col){
    printf("%c[%d;%dH",27,row,col);
}

清空螢幕:

void clearScreen(){
    printf("\e[1;1H\e[2J");
}

測試:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void locateCursor(const int row, const int col){
    printf("%c[%d;%dH",27,row,col);
}
void delay(unsigned int xms)  
{
    unsigned int x,y;
    for(x=xms;x>0;x--)
        for(y=1000000;y>0;y--);
}
int main()
{
    int i=0;
    system("clear");
    do{
        locateCursor(1,10);
        printf("%3d%%\n",i);
        delay(10);
        
    }while(i++<100);

}

結果: