清除终端程序 Linux C/C++ 的输出

标签 c linux terminal

我有兴趣清除使用 printf 语句生成的多行 C 程序的输出。

我最初的猜测是使用

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

但这会产生

 output1
 output2
 output3
 output4

我希望它能产生

 output3
 output4

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

最佳答案

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

#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++ 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1348563/

相关文章:

c++ - 具有毫秒精度的 Linux/OSX 时钟分辨率?

linux - Rabbitmq 服务器启动失败

bash - PS1 换行有颜色问题

bash - (macOS Bash) 2个看似相同的字符串并不相等,仅显示与 "set -x"的差异

c - 如何找出最后一次标记化的原因是什么

c - C 中的匹配哈希值

c - 为什么我的替换某个字符的函数只对该字符的第一个实例执行此操作?

c - 我在 A* 的 C 实现中正确使用了指针吗?

linux - 编译时出现 'Command Not Found' 错误

node.js - 在终端中停止 Gulp 时如何关闭 BrowserSync 的旧实例?