1

我似乎无法让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:这是因为我在打电话tgetent()之前没有打电话tgetstr()。多谢你们!

4

1 回答 1

3

在使用 询问之前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

于 2018-08-10T14:11:56.177 回答