Print spinning cursor in a terminal running application using C

后端 未结 5 987
Happy的楠姐
Happy的楠姐 2021-02-01 07:15

How would I print a spinning curser in a utility that runs in a terminal using standard C?

I\'m looking for something that prints: \\ | / - over and over in the same pos

5条回答
  •  醉酒成梦
    2021-02-01 08:21

    Here's some example code. Call advance_cursor() every once in a while while the task completes.

    #include 
    
    void advance_cursor() {
      static int pos=0;
      char cursor[4]={'/','-','\\','|'};
      printf("%c\b", cursor[pos]);
      fflush(stdout);
      pos = (pos+1) % 4;
    }
    
    int main(int argc, char **argv) {
      int i;
      for (i=0; i<100; i++) {
        advance_cursor();
        usleep(100000);
      }
      printf("\n");
      return 0;
    }
    

提交回复
热议问题