How to overwrite stdout in C

后端 未结 8 903
清歌不尽
清歌不尽 2020-12-07 18:13

In most modern shells, you can hit the up and down arrows and it will put, at the prompt, previous commands that you have executed. My question is, how does this work?!

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-07 18:36

    You can use carriage return to simulate this.

    #include 
    
    int main(int argc, char* argv[])
    {
        while(1)
        {
            printf("***********");
            fflush(stdout);
            sleep(1);
            printf("\r");
            printf("...........");
            sleep(1);
        }
    
        return 0;
    }
    

提交回复
热议问题