Studying about C++ I have found that it is not good to use the system() functions (like pause, or cls, etc)
I have two doubts:
Why is it not good to use them?
I found these two codes:
1.- Option:
void clear_screen()
{
DWORD n; /* Number of characters written */
DWORD size; /* number of visible characters */
COORD coord = {0}; /* Top left screen position */
CONSOLE_SCREEN_BUFFER_INFO csbi;
/* Get a handle to the console */
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
GetConsoleScreenBufferInfo ( h, &csbi );
/* Find the number of characters to overwrite */
size = csbi.dwSize.X * csbi.dwSize.Y;
/* Overwrite the screen buffer with whitespace */
FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
GetConsoleScreenBufferInfo ( h, &csbi );
FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
/* Reset the cursor to the top left position */
SetConsoleCursorPosition ( h, coord );
}
2.- Option:
void clrscr()
{
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = {0, 0};
DWORD count;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hStdOut, &csbi);
FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
SetConsoleCursorPosition(hStdOut, coord);
}
Which of the two would be the better replacement for the "cls" function?
Note: if you have a better code for this, it would be very useful if you could send it to me.
Normally, when a practice is discouraged in any writing or document, a reason for it is included. Read your source in more detail and they should explain why... unless you've read it on an internet forum, then that information could be taken for granted.
Without knowing the motivation or objective of the code, it is very difficult to know if something is advisable or not, but in general terms, using specific functions of the operating system is discouraged because it makes the code not portable . But that the code is not portable is only a problem in case you consider that it should be.
So by choosing OS-specific functionality (your code examples suggest that you're programming for the Windows console) you limit your code to that OS and make it incompatible with other systems; if this is what you want there is no problem, but if not, that would be the reason why it is not good to use those operating system specific functions.
No idea. Your code examples work with the console at a low level without delegating to calls to
system()
, if knowing your specific motivations it's hard to define if one approach is better than another.Without knowing more details, I think the option
clear_screen
works a little less than the oneclrscr
since the first one only overwrites the typed characters while the second one overwrites the entire screen.Basically four reasons:
You are invoking external programs over which you have no control. It can happen that different versions of the Operating System have different versions of the program, which can make your code not work.
They are OS dependent. Making calls to
system
makes it difficult for you to migrate the code between Windows and Linux (for example) since the program is probably not available in other OS.It is a very slow alternative. Having to run an external program
system
means that the console must interpret your input and run an external program... there are usually much faster and more reliable alternatives, either within the standard itself or by using third-party libraries.The capture of results is not obvious. Calling a console command to retrieve information for your application may not be obvious (the output may be affected by local system settings, for example). Calling native functions is usually a better solution since the data is often structured.
I assume both would work. As you can see, they are two almost identical solutions (let's say the first one is somewhat more complete). In any case, they are specific solutions for Windows environments.