954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Substitute for system("cls")`

The question is in the title. Rather then making a call to the OS, is there a more efficient way to do this? Thanks

Edit: Just though of something else to.
Does Sleep(2000); make a call to the OS as well? If so is there another way to do this as well?

clutchkiller
Junior Poster
183 posts since Jul 2008
Reputation Points: 23
Solved Threads: 3
 

Both sleep and cls are non-portable.

There is no portable method to clear the screen

Thats because you cant guarauntee the output device is nececerially the screen. e.g on an embedded device it could be a typewriter or something

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 

>is there a more efficient way to do this?
You mean without moving away from the console for non-sequential output? You could try pdcurses or something similar. At least that way you have more control, but if you're doing any kind of GUI-like interface or advanced drawing, you'd be better off taking the plunge into a modern graphics API.

>Does Sleep(2000); make a call to the OS as well?
How exactly are you expecting to talk to the system or any peripherals without making a call to the OS?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

> How exactly are you expecting to talk to the system or any peripherals without making a call to the OS?

On some operating systems and some devices, you can do just that.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

>On some operating systems and some devices, you can do just that.
Yes, but on the OP's operating system and device? Unlikely.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

not looking for a flame war guys lol. Thanks for the input.

clutchkiller
Junior Poster
183 posts since Jul 2008
Reputation Points: 23
Solved Threads: 3
 

No worries, Rash knows better than to get into a flame war with me. ;)

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Look at this link:
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1031963460&id=1043284385
The last method is probably what you're looking for.

unbeatable0
Junior Poster in Training
90 posts since Sep 2008
Reputation Points: 42
Solved Threads: 13
 

what about WinExec("cls"); ?

AHUazhu
Newbie Poster
17 posts since Dec 2008
Reputation Points: 10
Solved Threads: 2
 

Notes on system() and equivalent functions.

A guy walks up to a rack of fireworks blindfolded. After a short period of time, he mutters something and there's a huge explosion.

First Onlooker: What did he say?
Second Onlooker: I think it was "Is it lit yet?"

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

I think there is no flaw in using this system call but that it is platform dependent.
Also c++.com has no other function for the same
Actually you just write system("any dos command"); in windows.

But in linux::
1)u will have to include syscall.h
2)in linux the command to clearscreen is not cls but clear.

rohitfeb14
Newbie Poster
3 posts since Dec 2008
Reputation Points: 10
Solved Threads: 2
 

I think one good way to try to make system calls portable is to simply put them in a separate library file within a custom made function. You can call that function anywhere in your code and only really need to change the platform specific implementation that exists in the function definition. Any time you need to port the code you simply go to the library file, comment out the code that is being changed, and uncomment the code you want to use for the new platform.

zekish
Newbie Poster
12 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 
platform specific implementation
make system calls portable

If something is platform specific, then by definition it is non-portable.

Just skip the "cls", it takes away control from the user and can, IMO, become annoying. If you need that kind of control, use a portable library like pdcurses.

This thread is over 2 years old, by the way.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

To clear a terminal or console on any Unix/Linux system or other system with an ANSI, xterm, or DEC VT-100-compatible terminal, you can output a ^L (control-L) hex 0x0C (form-feed character) to stdout and that should do it. BTW, the system command to clear the screen on Linux systems is "clear", not "cls". Anyway, there isn't a simple one-line function that will do this on all systems, unfortunately!

rubberman
Posting Virtuoso
1,564 posts since Mar 2010
Reputation Points: 277
Solved Threads: 179
 

According to system function discussion. For me system("pause") is the best way to leave window open for Windows users to see results. On my system I just get info in terminal that this command was not found and program finishes normally. If someone would use appreciated (according to sticky) getchar, or cin.get, I would have to specially input character to finish a program. According to topic system("cls") is in my opinion the best option for beginner programmer to clear screen without having portability issues (of course you can use some #ifdefs and conio/curses/...). On systems without cls it will just output non fatal error, that cls program was not found.
@rubberman on debian and gnome-terminal trying to output 0xc just produces newline. Yes, maybe it is form feed, but DEC VT 100 time is gone.

Zjarek
Junior Poster in Training
79 posts since Oct 2009
Reputation Points: 20
Solved Threads: 18
 
According to system function discussion. For me system("pause") is the best way to leave window open for Windows users to see results.


And you'd be wrong.On my system I just get info in terminal that this command was not found and program finishes normally.
And this is why.If someone would use appreciated (according to sticky) getchar, or cin.get, I would have to specially input character to finish a program.
So? Thecharacter is the ENTER key. What's the big deal?

According to topic system("cls") is in my opinion the best option for beginner programmer to clear screen without having portability issues (of course you can use some #ifdefs and conio/curses/...).


And you'd be wrong again for the exact same reason. You have major portability issues, notnone as you state. And since you mentioned beginner programmers, you want to teach them "#ifdefs and conio/curses/..."? Get real!

On systems without cls it will just output non fatal error, that cls program was not found.


And this is acceptable? You've got to be just barely beyond beginner yourself to have this opinion...

If not and youare a professional, ask your boss if it's OK with him if you "output non fatal error, that cls program was not found" is acceptable in your project. I suspect we all know the answer, therefore it can't be the best solution.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
On systems without cls it will just output non fatal error, that cls program was not found.


The system function isn't as much a portability issue as it is a security hole. Anyone with half a brain can replace the program you're calling into with a malicious program of the same name and your code will happily execute it.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You