Termcap "cl" 命令不清除屏幕

Termcap "cl" command doesn't clear screen

我似乎无法使用 termcap 的 "cl" 命令,但终端转义码可以。

例如:

#include <termcap.h>
#include <stdio.h>

int main()
{
    tputs(tgetstr("cl", NULL), 1, putchar);
}

这不会更改终端。但是当我 运行:

#include <stdio.h>

int main()
{
    printf("\e[2J");
}

或者如果我打电话给 echo `tput cl` 终端清空

为什么会这样? termcap 不应该给出相同的转义码吗?

编辑:固定书写字符

EDIT2:这是因为我在调用 tgetstr() 之前没有调用 tgetent()。谢谢大家!

在用tgetstr()查询之前,需要用tgetent()找到用户终端的描述:

#include <stdio.h>
#include <stdlib.h>   // getenv
#include <termcap.h>  // tgetent tgetstr

int main(void)
{
    char buf[1024];
    char *str;

    tgetent(buf, getenv("TERM"));
    str = tgetstr("cl", NULL);
    fputs(str, stdout);
    return 0;
}

-ltermcap

编译