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

查看:34
本文介绍了清除终端程序 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
");
 printf("output2
");
 rewind(stdout);
 printf("output3
");
 printf("output4
");

但这会产生

 output1
 output2
 output3
 output4

我希望它会产生

 output3
 output4

有谁知道如何得到后者的结果?

Does anyone know how to get the latter result?

推荐答案

如果您还记得删除控制字符,则终端和管道都可以获得所需的结果.这是硬编码的两行.

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
",stdout);
    fputs("output2
",stdout);
    fputs("33[A33[2K33[A33[2K",stdout);
    rewind(stdout);
    ftruncate(1,0); /* you probably want this as well */
    fputs("output3
",stdout);
    fputs("output4
",stdout);
    return 0;
}

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

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