转:http://stackoverflow.com/questions/1022957/getting-terminal-width-in-c
方法一:
#include#include int main (void){ struct winsize w; ioctl(0, TIOCGWINSZ, &w); printf ("lines %d\n", w.ws_row); printf ("columns %d\n", w.ws_col); return 0;}
方法二:
#include#include #include #include static char termbuf[2048];int main(void){ char *termtype = getenv("TERM"); if (tgetent(termbuf, termtype) < 0) { error(EXIT_FAILURE, 0, "Could not access the termcap data base.\n"); } int lines = tgetnum("li"); int columns = tgetnum("co"); printf("lines = %d; columns = %d.\n", lines, columns); return 0;}
注意:Needs to be compiled with -ltermcap
. There is a lot of other useful information you can get using termcap. Check the termcap manual using info termcap
for more details.