Thread: How to know the number of lines in a text console

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.

  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
    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, 03: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, 03: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, 10:19 AM