Get size of terminal window (rows/columns)

前端 未结 6 2099
执笔经年
执笔经年 2020-12-02 13:37

Is there any reliable way of getting the number of columns/rows of the current output terminal window?

I want to retrieve these numbers in a C/C++ program.

I

6条回答
  •  再見小時候
    2020-12-02 14:14

    For Unix(-based), use ioctl(2) and TIOCGWINSZ:

    
    #include  //ioctl() and TIOCGWINSZ
    #include  // for STDOUT_FILENO
    // ...
    
    struct winsize size;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
    
    /* size.ws_row is the number of rows, size.ws_col is the number of columns. */
    
    // ...
    
    

    Also, while I haven't touched Windows in the last five years, GetConsoleScreenBufferInfo() should help you get the console window size.

提交回复
热议问题