C ++ Doubts about system ("cls")

1

Studying about C ++ I have found that it is not good to use the system () functions (such as 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 best replacement for the "cls" function?

Note: If you have a better code for this, it would be very useful for me to have it

    
asked by Malthael 25.02.2017 в 18:51
source

2 answers

3
  

Why is it not good to use them?

Four reasons basically:

  • You are invoking external programs over which you have no control. It may happen that different versions of the Operating System have different versions of the program, which can make your code not work.

  • They are dependent on the OS. Making calls to system makes it difficult to migrate the code between Windows and Linux (for example) since the program is probably not available in other OS.

  • It's a very slow alternative. Having to run an external program using system implies that the console must interpret your input and execute an external program ... there are usually much faster and more reliable alternatives, either within the standard itself or through 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 the local configuration of the system, for example). Calling native functions is usually a better solution since the data usually comes structured.

  •   

    Which of the two would be the best replacement for the "cls" function?

    I suppose the two would work. As you can see are two almost equal solutions (the first let's say it is something more complete). In any case, they are specific solutions for Windows environments.

        
    answered by 27.02.2017 / 09:36
    source
    4
      

    Studying about C ++ I have found that it is not good to use the functions system() (like pause , or cls , etc). Why is it not good to use them? .

    Normally, when in some writing or document a practice is discouraged, a motivation is included for it. Read your source in more detail and they should explain why ... unless you have read it in an internet forum, then that information could be given as known.

    Without knowing the motivation or objective of the code, it is very difficult to know if something is advisable or not, but generally speaking, using specific functions of the operating system is discouraged because it makes the code is not portable . But that the code is not portable is only a problem if you consider that it should be.

    So, choosing operating system-specific functionalities (your example codes indicate that you are programming for the Windows console) you limit your code to that operating system and make it incompatible for other systems; if this is what you want there is no problem, but otherwise that would be the reason why it is not good to use those functions specific to the operating system.

      

    Which of the two would be the best replacement for the " cls " function?

    No idea. Your example codes work with the console at a low level without delegating calls to system() , if knowing your specific motivations is difficult to define if one approach is better than another.

    Without knowing more details, I think that the option of clear_screen works a little less than the clrscr because the first only overwrites the written characters while the second overwrites the full screen.

        
    answered by 27.02.2017 в 09:48