C, system("cls"); not working, alternative?

Discussion in 'Mac Programming' started by darxun, Jun 7, 2008.

  1. darxun macrumors member

    Joined:
    Mar 18, 2008
    #1
    I am programming C with xCode. I tried these:
    Code:
    #include <curses.h>
    system("clear");
    But they are not working, system("clear") just puts a lot of space between the lines in terminal. And in the DebuggerConsole it says "TERM environment variable not set."
    So what can I do?
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    Learn the curses API would be a better idea. The system() function calls programs external to yours. You want to use the curses clear function.

    Plus don't forget to include the curses library in your Xcode project.

    This is an example curses program:

    Code:
    #include <ncurses.h>
    
    int main()
    {	int ch;
    
    	initscr();			/* Start curses mode 		*/
    	raw();				/* Line buffering disabled	*/
    	keypad(stdscr, TRUE);		/* We get F1, F2 etc..		*/
    	noecho();			/* Don't echo() while we do getch */
    
        	printw("Type any character to see it in bold\n");
    	ch = getch();			/* If raw() hadn't been called
    					 * we have to press enter before it
    					 * gets to the program 		*/
    	if(ch == KEY_F(1))		/* Without keypad enabled this will */
    		printw("F1 Key pressed");/*  not get to us either	*/
    					/* Without noecho() some ugly escape
    					 * charachters might have been printed
    					 * on screen			*/
    	else
    	{	printw("The pressed key is ");
    		attron(A_BOLD);
    		printw("%c", ch);
    		attroff(A_BOLD);
    	}
    	refresh();			/* Print it on to the real screen */
        	getch();			/* Wait for user input */
    	endwin();			/* End curses mode		  */
    
    	return 0;
    }
    Here is a decent guide:

    http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/index.html
     
  3. darxun thread starter macrumors member

    Joined:
    Mar 18, 2008
    #3
    Do I need all this Code just to clear my screen?
     
  4. PyroTurtle macrumors regular

    Joined:
    Jul 27, 2001
    Location:
    10 Minutes from Disneyland
    #4
    As a side note, in BSD (esp Darwin) that is all that clear does. It just makes it so that you don't see any of the output text by (basically) returning an entire screens worth of new lines basically. Annoying, ain't it?
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    it sounds like you want to make a commandline app but need more than a very rudimentary UI. If you want your app to be portable, using an established library instead of cobbling together some control characters that only work on OS X and maybe a few BSDs is a better idea.

    My guess is it will get messy fast if you're getting terminal geometry and trying to directly control everything.

    -Lee
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    Double post, sorry...

    The example posted does more than just clear the screen. The initscr() method is all that it takes. Perhaps this might serve as a word of warning, however... this is what ncurses printed to initalize the screen:
    Code:
    "\^[)0\^[7\^[[?47h\^[[1;56r\^[[m\^[[4l\^[[H\^[[2J"
    and this to clear it subsequently:
    Code:
    "\^[[H\^[[2J"
    and wrote this to restore it:
    Code:
    "\^[[56;1H\^[[2J\^[[?47l\^[8\r\^[[?1l\^[>"
    I have no idea what any of these control characters are... but ncurses handles all of this, per platform, for you.

    To try to simplify it, I wrote this:
    Code:
    #include <ncurses.h>
    
    int main(int argc, char *argv[]) {
      initscr();
      getch();
      printw("Muss up the screen a bit");
      getch();
      clear();
      getch();
      endwin();
      return 0;
    }
    
    The initscr() is all it takes to clear the screen. endwin() is all it takes to restore it to its previous state. The getch() is in there so it will stay with a blank screen until you press a key. I also put something on the screen and called clear again, to demonstrate that you can clear whenever you want once ncurses is up, not just at startup.

    I should state that there is definitely a way to do this by hand... but this makes it better. I have a pretty good feel for how this works, and I'd never looked at ncurses at all until 20 minutes ago.

    -Lee

    P.S. To compile this, which i called testncurses.c, i ran:
    Code:
    gcc -o testncurses -lncurses testncurses.c
    the -lncurses would be the only thing I wold expect to be unusual. That just adds in the ncurses library for the linker. I'm pretty sure this can be added easily to XCode to handle this for you, but I won't speak to things that I don't know for sure.
     
  7. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #7
    No. It was an example of a curses application. It was to show you that you need to use the curses functions such as clear() rather than the system() function.
     

Share This Page