Clearing output of a terminal program Linux C/C++

后端 未结 7 537
盖世英雄少女心
盖世英雄少女心 2020-11-27 14:29

I\'m interested in clearing the output of a C program produced with printf statements, multiple lines long.

My initial guess was to use

 printf(\"ou         


        
相关标签:
7条回答
  • 2020-11-27 15:29

    You can have the desired result both for terminal and pipes if you remember to remove the control characters as well. This is hardcoded for two lines.

    #include <stdio.h>
    
    int
    main ()
    {
        fputs("output1\n",stdout);
        fputs("output2\n",stdout);
        fputs("\033[A\033[2K\033[A\033[2K",stdout);
        rewind(stdout);
        ftruncate(1,0); /* you probably want this as well */
        fputs("output3\n",stdout);
        fputs("output4\n",stdout);
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题