关于转义字符\r
轉義字符"\r"的作用是把光標移到當前的行首,而轉義字符"\n"是把光標下一行首。
我們來做一個簡單的應用,利用轉義字符"\r"動態地在終端顯示時間。
#include <iostream> #include <string.h> #include <stdio.h> #include <windows.h> #include <time.h>using namespace std;int main() {time_t t;char date[35];while(1){t = time(NULL);strcpy(date,ctime(&t));int len = strlen(date);date[len-1] = 0;printf("\r%s",date);Sleep(500);}return 0;}
上面的char *ctime(const time_t *t);函數的作用是把真實的時間轉化為字符串形式。
總結