1,105,417 Community Members

system("cls")

Member Avatar
azwraith69
Newbie Poster
21 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hello.. thx in advance

just want to ask something..

is there any portable functions that is equivalent to system("cls")?
a function that clears the screen? since system() is not portable..

thx...

Member Avatar
Ancient Dragon
Achieved Level 70
27,645 posts since Aug 2005
Reputation Points: 5,234 [?]
Q&As Helped to Solve: 3,038 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

system() is portable -- the commands it execute are not. There are no portable ways to clear the screen. You might have to do something like this, where each compiler for the target os defines one of the symbols listed in the code below.

#ifdef _UNIX_
   system("clear");
#elif def _WINDOWS_
   system("cls");
#elsif def _OTHER_OPERATING_SYSTEM
   systgem(<put here whatever works>);
#endif
Member Avatar
jbennet
Moderator
17,127 posts since Apr 2005
Reputation Points: 1,618 [?]
Q&As Helped to Solve: 736 [?]
Skill Endorsements: 38 [?]
Team Colleague
Featured
 
0
 

no.

Member Avatar
Duoas
Postaholic
2,039 posts since Oct 2007
Reputation Points: 1,022 [?]
Q&As Helped to Solve: 229 [?]
Skill Endorsements: 10 [?]
Featured
 
0
 

Why do you want to do that?
(In other words, unless you are writing a full-screen text application like an editor or video game, don't do that.)

Otherwise, on Windows, see Example Two

On Unix/Linux/POSIX:

#include <unistd.h>
#include <term.h>

void clearscreen()
  {
  if (!cur_term)
    {
    int success;
    setupterm( NULL, STDOUT_FILENO, &success );
    if (success <= 0) return;
    }
  putp( tigetstr( "clear" ) );
  }

You'll need to link with one of the following (depending on your system):
-lcurses
-lncurses
-lterminfo

If you are writing a CUI application, though, you should just use Curses directly:

PDCurses
Windows, DOS, OS/2, Plan 9, etc
http://pdcurses.sourceforge.net/

NCurses
POSIX systems, Mac OS X, etc
http://www.gnu.org/software/ncurses/

Getting started
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
http://web.cs.mun.ca/~rod/ncurses/ncurses.html

Hope this helps.

Member Avatar
hahanottelling
Newbie Poster
16 posts since Jan 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Couldn't you just print a lot of newlines and then move the cursor to the top of the window?

Member Avatar
Ancient Dragon
Achieved Level 70
27,645 posts since Aug 2005
Reputation Points: 5,234 [?]
Q&As Helped to Solve: 3,038 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

You could, but that isn't portable either. And how many newlines would you print? How would you move the cursor?

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article