2

I am using MS visual studio 2008, for C coding.

I know we can use "int system(const char *command)" to execute commands.

Is there any other method to execute system commands in C program. Also I need to store output of executed command in a variable.

system() function execute command and send output to stdout , is there any way to read from stdout and store in variable.

So my ultimate goal is to execute system command in C program for windows (using visual studio) and store output of that command in a variable. Any suggestions ?

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
Ashish Rana
  • 95
  • 1
  • 2
  • 8
  • 1
    In standard C? There's no other way. If you want platform-specific functions to be able to read the output of the command, then do some research about the higher-level `_popen` and lower-level `CreateProcess` functions. Both are Windows specific functions. – Some programmer dude Jul 18 '17 at 06:43
  • Use system() and pipe the output into a temp file, Open and read the temp file. – Martin James Jul 18 '17 at 07:09

2 Answers2

5

Standard C libraries give you only one way to execute external command in OS, so use int system(const char *command).

You can save output of this command to text file, and then read this file from you program.

For example:

#include <stdio.h>
#include <stdlib.h>

#define TMP_FILE_NAME "TMP_FOLDER_CONTENT.txt"

int main(int argc, char *argv[])
{
    system("dir C:\* > "TMP_FILE_NAME);
    FILE * fdir = fopen(TMP_FILE_NAME, "r");
    char buff[100];
    if (fdir)
    {
        while (1) {
            if (fgets(buff, 100, fdir) == NULL) break;
            printf("%s", buff);
        }
    }
    fclose(fdir);
    remove(TMP_FILE_NAME);
    return 0;
}

Where dir is a program to be executed, C:\* - argument of the program, and > - redirection of standard output for that command after which filename TMP_FOLDER_CONTENT.txt will be substituted.

Also you can check returned value, as:

int errorcode = system("dir C:\* > "TMP_FILE_NAME);
printf("Command executed and returned a value %d\n", errorcode);

or taking into account command you use, change the logic of your program, e.g.:

int errorcode = system("dir C:\* > "TMP_FILE_NAME);
if( errorcode )
{
   return errorcode;
}

UPDATE:

Alternatively, you could use pipes in C++, for example as shown in the answer to question How to execute a command and get output of command within C++ using POSIX?

VolAnd
  • 6,367
  • 3
  • 25
  • 43
  • Is there any way so i dont need extra file, I mean, write output directly to variable. – Ashish Rana Jul 18 '17 at 07:07
  • Unfortunately, you cannot to set your program as command line command output receiver instead of `stdout`, and more problem is in the way of execution of `system` command - in my example it is synchronous command execution and next command can be executed only after system is completed and returned a value – VolAnd Jul 18 '17 at 07:27
  • @AshishRana I updated the answer with reference to similar question - it can help, I suppose – VolAnd Jul 18 '17 at 07:43
0

you can do as @VolAnd said or also if you don't care about/don't want the output of the command to be in stdout and you also don't want anything else to be printed to stdout you can use freopen to set stdout to a file of your choice.

zee
  • 2,933
  • 2
  • 16
  • 28