终端程序Linux下C / C的清除输出++ [英] Clearing output of a terminal program Linux C/C++

查看:909
本文介绍了终端程序Linux下C / C的清除输出++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感兴趣的清理与printf的语句,多线长产生一个C程序的输出。

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

我最初的猜测是使用

 printf("output1\n");
 printf("output2\n");
 rewind(stdout);
 printf("output3\n");
 printf("output4\n");

但产生

 output1
 output2
 output3
 output4

我希望它会产生

 output3
 output4

有谁知道如何让后者的结果呢?

Does anyone know how to get the latter result?

推荐答案

可以,如果你还记得删除控制字符,以及同时具有终端和管道预期的效果。这是两行硬codeD。

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

这篇关于终端程序Linux下C / C的清除输出++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆