Question

I am trying to make a clock in C, but the screen is not properly clearing, it just keeps printing to a new line. How am I improperly using 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;
}
Was it helpful?

Solution

This strips out the newline from the asctime string and then uses a return to push the cursor back to the start of line

#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;
}

OTHER TIPS

This one has the advantage that it will work from the current location on the screen, no matter what that is. I added a label to print "The time is: " in order to show this. It does this by back spacing from the end of the time string rather than going to an absolute screen position or column. Caveat: The hack to get sleep() under Visual C has not been tried.

#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;
}

This will do it... (uses an evil windows call SetConsoleCursorPosition(), but does the trick)

#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);
}

Try this for 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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top