Print spinning cursor in a terminal running application using C

后端 未结 5 983
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:13

    I've done that, long ago. There are two ways.

    1. Use a library like ncurses to give you control over the terminal. This works well if you want to do a lot of this kind of stuff. If you just one this in one little spot, it's obviously overkill.

    2. Print a control character.

    First you print "/", then 0x08 (backspace), then "-", then 0x08, then "\"....

    The backspace character moves the cursor position back one space, but leaves the current character there until you overwrite it. Get the timing right (so it doesn't spin to fast or slow) and you're golden.

提交回复
热议问题