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'm looking for a GNU/Linux solution primarily, but also need a Windows solution.
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'm looking for a GNU/Linux solution primarily, but also need a Windows solution.
On GNU/Linux using libtermcap (https://www.gnu.org/software/termutils/manual/termcap-1.3/html_mono/termcap.html) create demo.c:
Then compile with
gcc -o demo.x demo.c -ltermcap
and run to give:I doubt this helps much on Windows though, I don't know that platform.
(Some of this code is copied straight from the termcap documentation.)
Linux/unix: Use
ioctl
with the standard output file numberSTDOUT_FILENO
andTIOCGWINSZ
.Also, while I didn't touch windows in the last 5 years,
GetConsoleScreenBufferInfo
should help you get the current console size.On Windows, use the following code to print the size of the console window (borrowed from here):
On Linux, use the following instead (borrowed from here):
To expand @herohuyongtao answer for Windows. The .srWindow property gives the answer to the size of the console window, i.e. visible rows and cols. This doesn't say what is the actual available screen buffer width and height, which could be larger if window contains scroll bars. If this is the case, use .dwSize: