Termcap “cl” command doesn't clear screen

寵の児 提交于 2021-01-28 07:05:42

问题


I can't seem to get termcap's "cl" command to work, but the terminal escape code does.

For example:

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

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

This doesn't change the terminal. But when I run:

#include <stdio.h>

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

or if I call echo `tput cl` The terminal is cleared.

Why does this happen? Shouldn't termcap give that same escape code?

EDIT: Fixed writing characters

EDIT2: It's because i didn't call tgetent() before calling tgetstr(). Thanks guys!


回答1:


Before interrogating with tgetstr(), you need to find the description of the user's terminal with tgetent():

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

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

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

Compile with -lncurses



来源:https://stackoverflow.com/questions/51785373/termcap-cl-command-doesnt-clear-screen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!