获取终端窗口的大小(行/列) [英] Get size of terminal window (rows/columns)

查看:111
本文介绍了获取终端窗口的大小(行/列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何可靠的方法来获取当前输出终端窗口的列数/行数?

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

我想在C / C ++程序中检索这些数字

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

我主要是在寻找GNU / Linux解决方案,但也需要Windows解决方案。

I'm looking for a GNU/Linux solution primarily, but also need a Windows solution.

推荐答案

对于Unix(基于),请使用 ioctl(2) TIOCGWINSZ

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


#include <sys/ioctl.h> //ioctl() and TIOCGWINSZ
#include <unistd.h> // 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. */

// ...

虽然我过去五年没有接触过Windows,但 GetConsoleScreenBufferInfo() 应该可以帮助您获取控制台窗口的大小。

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

这篇关于获取终端窗口的大小(行/列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆