How to know the number of lines in a text console

This is a discussion on How to know the number of lines in a text console within the Linux Programming forums, part of the Platform Specific Boards category; Hi, How can I know the number of lines of the screen when working in a text environment (non X ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    13

    How to know the number of lines in a text console

    Hi,

    How can I know the number of lines of the screen when working in a text environment (non X Windows).

    Thanks in advance

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    You can't - in standard C anyway.

    You could try using a library like ncurses, but that adds quite a bit to your code.

    First question is, why do you want to know?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    .
    Join Date
    Nov 2003
    Posts
    307
    Without messing around with terminal calls, most modern unix shells
    (POSIX (sh) and Korn (ksh) ) support the LINES variable

    A crude hack:

    Code:
    #include <stdio.h>
    
    int MAX_LINES(void)   // number of terminal lines
    {
    	
    	char tmp[24]={0x0};
    	FILE *p=popen("echo $LINES","r");
    	fgets(tmp,sizeof(tmp),p);
    	return atoi(tmp);
    }
    This is not something to call repeatedly, it creates a process, so is slow

  4. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    why not just use getenv("LINES");
    hello, internet!

  5. #5
    .
    Join Date
    Nov 2003
    Posts
    307
    moi - yes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  3. Number of words multiplied by the number of lines in Linux
    By sjalesho in forum Linux Programming
    Replies: 2
    Last Post: 11-20-2003, 02:25 PM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 09:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21