Trên Windows, sử dụng đoạn mã sau để in các kích thước của cửa sổ giao diện điều khiển (mượn từ here):
#include <windows.h>
int main(int argc, char *argv[])
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
int columns, rows;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
printf("columns: %d\n", columns);
printf("rows: %d\n", rows);
return 0;
}
Trên Linux, sử dụng sau đây để thay thế (mượn từ here):
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
int main (int argc, char **argv)
{
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
printf ("lines %d\n", w.ws_row);
printf ("columns %d\n", w.ws_col);
return 0; // make sure your main returns int
}
Trên Linux, bạn cũng có thể thử 'stty size' – choroba
[này] (http://stackoverflow.com/questions/18783988/how-to-get- windows-size-from-linux) có thể hữu ích. – devnull
Có thể trùng lặp của [Nhận chiều rộng đầu cuối trong C?] (Https://stackoverflow.com/questions/1022957/getting-terminal-width-in-c) –