Get console screen size in Windows and Linux

Console application in both operating systems Windows and Linux measure the screen size using the number of columns and lines instead of use pixels. This because console mode allow only characters output inside the ASCII range and each character block have a fixed width and height size.

Get the console size (number of columns and lines) is a very easy task using the following snippets.

Code for Windows


GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);

Height = (csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
Width  = (csbi.srWindow.Right - csbi.srWindow.Left + 1);

Code for Linux

struct winsize ws;

ioctl(0, TIOCGWINSZ, &ws);

Height = ws.ws_row;
Width  = ws.ws_col;

Simply copy and past in your project code.


Popular posts from this blog

Android: adb push and read-only file system error

Access GPIO from Linux user space