问题
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