亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用C來獲取終端寬度?

用C來獲取終端寬度?

LEATH 2019-07-11 20:58:13
用C來獲取終端寬度?我一直在尋找從我的C程序中獲得終端寬度的方法。我一直想出的東西是:#include <sys/ioctl.h>#include <stdio.h>int main (void){     struct ttysize ts;     ioctl(0, TIOCGSIZE, &ts);     printf ("lines %d\n", ts.ts_lines);     printf ("columns %d\n", ts.ts_cols);}但每次我試著austin@:~$ gcc test.c -o test test.c: In function ‘main’:test.c:6: error: storage size of ‘ts’ isn’t known test.c:7: error: ‘TIOCGSIZE’ undeclared (first use in this function)test.c:7: error: (Each undeclared identifier is reported only once test.c:7: error: for each function it appears in.)這是最好的方法,還是有更好的方法?如果不是,我怎樣才能讓它發揮作用?編輯:固定代碼是#include <sys/ioctl.h>#include <stdio.h>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;}
查看完整描述

3 回答

?
慕絲7291255

TA貢獻1859條經驗 獲得超6個贊

#include <stdio.h>#include <stdlib.h>#include <termcap.h>#include <error.h>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;}

需要用-ltermcap..有很多其他有用的信息,你可以使用術語。檢查術語手冊info termcap更多細節。


查看完整回答
反對 回復 2019-07-11
  • 3 回答
  • 0 關注
  • 939 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號