Executing System Commands Using C++ code

System commands can be exexuted using C++. Sometimes it is needed when some information about the system is to be gathered by the program or some other task needs to be performed. Here’s the code that can be used to execute system commands using C++.

/* system example : DIR */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* system, NULL, EXIT_FAILURE */

int main()
{
    int i;
    printf("Checking if processor is available...");
    if (system(NULL)) puts("Ok");
    else exit(EXIT_FAILURE);
    printf("Executing command DIR...\n");
    i = system("dir");
    printf("The value returned was: %d.\n", i);
    return 0;
}

This content has been helpful to you?

Thanks for contributing!

Yes No