Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C - <b>system() execute dos command</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
system()                 Execute DOS Command

 #include   <process.h>                  Required for declarations only
 #include   <stdlib.h>                   Use either process.h or stdlib.h

 int        system(string);
 char       *string;                     Command to be executed

    system() passes 'string' to the command interpreter COMMAND.COM to be
    executed. This executes 'string' as if it were typed in at the DOS
    prompt.

    Returns:    0 if the command is successfully executed. -1 indicates
                an error and 'errno' (defined in <stdlib.h>) is set to
                one of the following values (defined in <errno.h>):

                    E2BIG      The argument list for the command exceeds
                               128 bytes.
                    ENOENT     COMMAND.COM cannot be found.
                    ENOEXEC    COMMAND.COM is not an executable file.
                    ENOMEM     Not enough memory.

      Notes:    system() uses the COMSPEC and PATH environment variables
                to locate COMMAND.COM.

  -------------------------------- Example ---------------------------------

    The following statements send a directory listing to the file
    "dir.txt":

            #include <process.h>     /* for 'system' */
            #include <stdio.h>       /* for 'printf' */
            #include <stdlib.h>      /* for 'errno' */
            #include <errno.h>       /* for 'ENOENT' and 'ENOMEM' */

            main()
            {
                int result;

                result = system("dir >> dir.txt");
                if (result == -1)
                    if (errno == ENOENT)
                        printf("can't find COMMAND.COM\n");
                    else if (errno == ENOMEM)
                        printf("not enough memory\n");
                    else
                        printf("error #%d executing system command\n",errno);
            }

See Also: exec...() spawn...()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson