这样就可以了...(使用邪恶的 windows 调用SetConsoleCursorPosition()
,但可以解决问题)
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void gotoxy(int x, int y);
int main()
{
while (1) {
time_t rawtime;
struct tm * timeinfo;
gotoxy(0,0);//set to the upper left hand corner
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ("%s", asctime (timeinfo));
fflush(stdout);
}
return 0;
}
void gotoxy(int x, int y)
{
COORD pos = {x, y};
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(output, pos);
}
为 POSIX 试试这个:
#!/usr/bin/tcc -run
#include <stdio.h>
#include <termios.h>
int main ()
{
struct termios ts0, ts1;
char cls [FILENAME_MAX];
FILE *f;
f = popen ("tput clear", "r");
fgets (cls, FILENAME_MAX, f);
pclose (f);
tcgetattr (0, &ts0);
ts1 = ts0;
ts1.c_lflag &= ~ECHO;
ts1.c_lflag &= ~ICANON;
tcsetattr (0, TCSAFLUSH, &ts1);
fputs (cls, stdout);
while (1) putchar (getchar ());
tcsetattr (0, TCSAFLUSH, &ts0);
return 0;
}