SORU
21 HAZİRAN 2009, Pazar


C alma, terminal genişliği?

Bir C programı içinden terminal genişliğini almak için aradım. Karşıma çıkan ne çizgisinde bir şey

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

int main (void)
{
    struct ttysize ts;
    ioctl(0, TIOCGSIZE, &ts);

    printf ("lines %d\n", ts.ts_lines);
    printf ("columns %d\n", ts.ts_cols);
}

Ama her zaman almak deneyin

austin@:~$ gcc test.c -o test
test.c: In function main’:
test.c:6: error: storage size of ts isnt known
test.c:7: error: TIOCGSIZE undeclared (first use in this function)
test.c:7: error: (Each undeclared identifier is reported only once
test.c:7: error: for each function it appears in.)

Bu en iyi yolu bunu yapmak için, yoksa daha iyi bir yolu var mı? Bunu nasıl çalıştırabilirim?

EDİT: sabit kod

#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;
}

CEVAP
21 HAZİRAN 2009, Pazar


getenv() kullanarak düşündünüz mü ? Sen terminalleri sütunlar ve satırlar içeren sistem ortam değişkenleri almak için izin verir.

Alternatif olarak kullanma yöntemi, görmek istiyorsan ne çekirdek görür terminal boyutu (iyi durumda terminal yeniden boyutlandırıldığında) size kullanmaya ihtiyaç TİOCGWİNSZ karşıt olarak, sizin TİOCGSİZE, bunun gibi

struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

ve tam kodu:

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

int main (int argc, char **argv)
{
    struct winsize w;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

    printf ("lines %d\n", w.ws_row);
    printf ("columns %d\n", w.ws_col);
    return 0;  // make sure your main returns int
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • HowcastSportsFitness

    HowcastSport

    11 Mayıs 2011
  • LardTardProductions's channel

    LardTardProd

    10 NİSAN 2009
  • The Fashion Sight

    The Fashion

    22 AĞUSTOS 2011