22

I have seen a lot of people on forums telling to avoid the system() function, like system("cls"). I don't understand why.

Please tell me why I should avoid this function. And also, as clrscr() doesn't work with CodeBlocks, what are other ways to clear screen without using the system() function?

manlio
  • 18,345
  • 14
  • 76
  • 126
Shubham
  • 935
  • 1
  • 7
  • 25
  • It's `system`, not `System`, and `clrscr` isn't a standard function. There is no standard way to clear it, but I'll bet it's not the IDE that `clrscr` doesn't work with. – chris Nov 11 '13 at 18:41
  • It probably is `system` (lower-case letters). And the `cls` command is operating-system specific (does not exist on Linux), and could have been removed or renamed by the sysadmin... – Basile Starynkevitch Nov 11 '13 at 18:41
  • 5
    it's not portable; what works on windows might not work on linux or mac. – stellarossa Nov 11 '13 at 18:42
  • You might be interested by [ncurses](http://en.wikipedia.org/wiki/Ncurses) at least on Linux. – Basile Starynkevitch Nov 11 '13 at 18:46
  • 7
    `system` is a call that accepts any kind of unauthenticated shellcode, and it invokes a command interpreter that you usually do not want. Insofar it's extra overhead for adding a possible security exploit to your program. – Damon Nov 11 '13 at 18:50
  • It can cause a security vulnerability: https://www.securecoding.cert.org/confluence/display/seccode/ENV04-C.+Do+not+call+system()+if+you+do+not+need+a+command+processor – Andreas Fester Nov 11 '13 at 18:51
  • 1
    Why do you want to clear the screen? There might be useful information there. – Keith Thompson Apr 21 '15 at 07:09
  • You should avoid system-dependent code in code intended to be a portable. But there's no reason to avoid `system` in general. The situation for `system` is much the same as for using the arguments of `main`: it's system dependent, so you'd better know what you're doing, but it would be silly to avoid it in general. – Cheers and hth. - Alf Feb 17 '17 at 15:59

3 Answers3

32

There are multiple problems here:

  • First of all, system() as a function is cross-platform and available not just on Windows or Linux. However, the actual programs being called might be platform dependant. For example, you can use system() to create a directory: system("md Temp"). This will only work on Windows, as Linux doesn't know a command called md. For Linux it would have to be system("mkdir Temp"). This goes on, so you'd need a custom solution for each and every platform.
  • This will always spawn a child process that's then executing something. This will in general be slower than some inlined code, e.g. the command or program has to be loaded, has load it's own dependencies, then it has to be executed etc. which is usually a lot more work.

If you're just doing some quick testing on one platform, using system() is perfectly fine, but you shouldn't use it in production environments, unless you really have to. For example, you could allow the user to set an external program that is then executed. For something like this system() is perfectly fine.

Mario
  • 35,726
  • 5
  • 62
  • 78
  • In addition: If you have a dependency on a certain program (eg for displaying results) a system call might be preferable. –  Nov 11 '13 at 19:00
  • 1
    Upvoted because you explain that it depends on context. It is the same with your example to let the system set external programs. Could be ok sometimes. Sometimes cross paltform is not an issue either. – user2672165 Nov 11 '13 at 19:01
  • also it is blocking system call. – Arya Sep 03 '16 at 18:09
4

There is an answer about system() usage. And there is no standard C++ way to clear console window. For Windows platform you can use such code:

void clear() 
{
    COORD startPos  = { 0, 0 };
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO screen;
    DWORD written;

    GetConsoleScreenBufferInfo(hConsole, &screen);
    FillConsoleOutputCharacterA(hConsole, ' ', screen.dwSize.X * screen.dwSize.Y, startPos, &written);
    FillConsoleOutputAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE, screen.dwSize.X * screen.dwSize.Y, startPos, &written);
    SetConsoleCursorPosition(hConsole, startPos);
}

And for linux, never tried, but found the way:

#include <curses.h>
erase();
ST3
  • 8,826
  • 3
  • 68
  • 92
1

You should avoid system call because

  • Those calls are not portable, they might not work on other platforms.
  • Those calls are expensive to call, why would you let your resources got eaten?

How to clear your console? You can use std::cout << std::string(50, '\n');

igleyy
  • 605
  • 4
  • 16
  • 4
    *"You need to include some headers to use such functions."* - That's actually something you'll always have, no matter which library you're using. Also just printing lots of line breaks won't necessarily clear your screen properly. For example, under Windows nowadays most console windows have a buffer of about 300 lines. – Mario Nov 11 '13 at 18:52
  • I already removed last bullet, that's completely true what you say. – igleyy Nov 11 '13 at 18:54
  • 3
    `cout << string(300, '\n');` checkmate @Mario – stellarossa Nov 11 '13 at 18:59
  • Wouldn't you also want to flush to be sure? – user2672165 Nov 11 '13 at 19:05
  • 6
    @stellarossa: That will create 300 empty lines in your scrollback or when redirecting output to a file. Still not really elegant to use. ;) – Mario Nov 11 '13 at 19:09
  • ... and not every console has less than 50 rows – phuclv Apr 21 '15 at 05:46
  • @Mario, also console has scrollbar, and even if output enough lines, you'll get to the bottom instead of the top. And I configured my console to have 9999 lines (maximum possible on windows). – Qwertiy Jan 11 '19 at 15:25
  • @Qwertiy Correct, but that's even the case when invoking system commands like `clear`. If you scroll up, stuff will appear again, there's no real guarantee for this to work (although it might work in one environment, it won't necessarily work in the other). – Mario Jan 12 '19 at 16:04
  • 1
    @Mario, nope. On windows `cls` command really clears console and scrolls to top. – Qwertiy Jan 12 '19 at 18:39