开发者

How can I find the number of terminal columns from a C/C++ program? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Getting terminal width in C?

On Linux and OS X, my shell reports $COLUMNS has the width of the terminal window -- and resizing开发者_如何学Go the window will adjust this shell variable.

But in my C/C++ program, getenv("COLUMNS") doesn't seem to find the variable.

Anybody have an explanation? Or an alternate suggestion for letting my C++ program figure out the width of the terminal it's running in (for some help message word wrapping)?


Perhaps something like this:

#include <sys/ioctl.h>
#include <stdio.h>

int main()
{
    struct winsize w;
    ioctl(0, TIOCGWINSZ, &w);

    printf("lines %d\n", w.ws_row);
    printf("columns %d\n", w.ws_col);
    return 0;
}

Taken straight from: Getting terminal width in C?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜