2013-10-26 1 views
0

C로 시계를 만들려고하지만 화면이 제대로 지워지지 않고 계속 새로운 라인으로 인쇄됩니다. fflush을 부적절하게 사용하려면 어떻게해야합니까?fflush를 사용하는 시계가 화면을 지우고 있지 않습니다.

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

int main() 
{ 
    while (1) { 

     time_t rawtime; 
     struct tm * timeinfo; 

     time (&rawtime); 
     timeinfo = localtime (&rawtime); 
     printf ("%s", asctime (timeinfo)); 
     fflush(stdout); 

    } 
    return 0; 
} 
+0

fflushfflush는 stdoutstdout에 쓰는 내용이 실제로 표시되는지 확인합니다. 화면을 지우는 것과 아무런 관련이 없습니다. '\ r'을 사용하여 문자열 앞에 줄을 써서 같은 줄에 출력을 다시 쓸 수 있습니다. –

+0

화면을 지우려면 코드를 작성하여 화면을 지워야합니다. C는 사용자가 화면이 있거나 그것이 지워질 수 있다고 가정하지 않으므로, 사용자가 가지고있는 특정 플랫폼이나 터미널에 적합한 방식으로해야합니다. –

답변

2

이것은 asctime과 문자열에서 줄 바꿈을 제거합니다 다음이 그것을 할 것입니다

#include <string.h> 

int main() 
{ 
     while (1) { 

        time_t rawtime; 
        char st[30]; 
          struct tm * timeinfo; 

            time (&rawtime); 
            timeinfo = localtime (&rawtime); 
            sprintf (st,"%s", asctime (timeinfo)); 
            *(index(st,'\n'))='\0'; 
            printf("\r%s",st); 
            flush(stdout); 
            sleep(1); 

        } 
            return 0; 
} 
+0

에는'index (st, '\ n')'가 필요합니다. ? – ryyker

0

라인의 시작 부분으로 커서를 뒤로 밀어 복귀를 사용 ... (악마의 창을 사용하여 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; 
} 
+0

나는 윈도우가 없지만 ncurses가 비슷한 것을 가지고 있다고 생각한다. –

+0

태그에서 어떤 시스템을 사용하고 있었는지 알 수는 없었지만, 예, (n) unix'esk에 대한 저주가 트릭을 수행 할 수 있습니다. 죄송합니다 도움이되지 못했습니다 :) – ryyker

+0

당신의 대답은 문제 해결의 대안을 제시해주었습니다. 감사합니다. 제 시스템은 Mac과 android입니다. –

2

이 항목은 화면의 현재 위치와 상관없이 작동 할 수 있다는 장점이 있습니다. 이것을 보여주기 위해 "The time is :"이라는 글자를 추가했습니다. 이는 절대 화면 위치 또는 열로 이동하지 않고 시간 문자열의 끝에서 뒤로 간격을 두어이를 수행합니다. 주의 사항 : Visual C에서 sleep()을 시도하는 해킹이 시도되지 않았습니다.

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <string.h> 
#ifdef _MSC_VER 
    #include <windows.h> 
    #define sleep(T) Sleep((T) * 1000) 
#else 
    #include <unistd.h> 
#endif 

int main(void) 
{ 
    char buf[42]; 
    time_t the_time[1]; 
    int i, len; 

    printf("The time is: "); 
    for (;;) { 
    time(the_time); 
    len = strlen(strcpy(buf, asctime(localtime(the_time)))) - 1; 
    printf("%.*s", len, buf); 
    for (i = 0; i < len; i++) putchar('\b'); 
    fflush(stdout); 
    sleep(1); 
    } 
    return 0; 
} 
+0

Windows와 Unix/Linux 간에는 매우 유용합니다. – ryyker

+0

@ryyker 나는 그렇게 생각한다. 하지만 저는 리눅스가 편리하지 않습니다. Windows cmd와 mingw bash는 모두 작동합니다. – Gene

+0

예, 좋습니다. 또한 Linux에서도 작동하지만 Linux에서 unistd.h가 필요하기 때문에 sleep()의 "* implicit decalration *"에 대해 경고합니다. 그러나 ''\ x08 ''대신 ''\ b ''을 사용하지 않으시겠습니까? 조금 더 직관적으로 읽을 수 있습니다. – alk

관련 문제