我最近试图获得运行该程序的终端的大小.
问题是脚本+程序解决方案不方便,它不支持用户输入或终端大小更改.
我怎么能在C++代码中做到这一点(没有弄乱脚本)?
我的代码:
Bash文件(要运行的文件而不是C++程序):
cols=`tput cols`
lines=`tput lines`
input="${cols}\n${lines}"
echo -ne "${input}" | [C++ program]
Run Code Online (Sandbox Code Playgroud)
C++文件(实际程序):
#include <iostream>
int main() {
unsigned lines;
unsigned cols;
cin >> cols;
cin >> lines;
// some code here
}
Run Code Online (Sandbox Code Playgroud)
如图所示
你可以通过ioctl调用获得终端大小
#include <sys/ioctl.h>
#include <stdio.h>
int main (void)
{
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
printf ("lines %d\n", w.ws_row);
printf ("columns %d\n", w.ws_col);
return 0;
}
Run Code Online (Sandbox Code Playgroud)