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

Possible duplicate:
Getting terminal width in C?

On Linux and OS X, my $ COLUMNS shell messages have a terminal window width - and resizing the window will change this shell variable.

But in my C / C ++ program, getenv ("COLUMNS") does not seem to find the variable.

Does anyone have an explanation? Or an alternative suggestion for my C ++ program to determine the width of the terminal on which it is running (for any help).

+5
source share
1 answer

Maybe 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 directly from: Getting terminal width in C?

+10

All Articles