9

How can I view the output of a system command. Ex:

int _tmain(int argc, _TCHAR* argv[]) {

   system("set PATH=%PATH%;C:/Program Files (x86)/myFolder/bin");
   system("cd C:/thisfolder/");

   std::cin.get();
   return 0;

}

when I run the program in Visual Studio it give me a black screen and I cannot see the command being run. I need it so I can view whether it worked or not. Thanks!

BlueElixir
  • 3,299
  • 8
  • 20
  • 23
  • Probably an output redirection issue. The `system()` function uses the command interpreter to run the argument string of the `system() `function so it may be that the command interpreter is not inheriting your standard out so the output from the command is not going to the console window. Another thing may be that the commands you are using are not generating any output. try a different command such as the `echo` command so do something like `system("echo this is output")`. – Richard Chambers Jan 22 '15 at 15:58
  • 2
    I am not sure that `set`-ing (or `cd`) with one call to `system` would affect the next call to `system` (on POSIX & Linux, I am sure it does not work. But I don't know Windows). – Basile Starynkevitch Jan 22 '15 at 16:33
  • 1
    Did you consider using some OS API (e.g. [_chdir](https://msdn.microsoft.com/en-us/library/bf7fwze1.aspx)...) instead of calling `system` ? Or use some framework like [POCO](http://pocoproject.org/), [Qt](http://qt-project.org/), or perhaps Boost. – Basile Starynkevitch Jan 22 '15 at 16:35
  • 2
    @BasileStarynkevitch you are correct. Since the set command is run in the command interpreter once the command interpreter exits, any changes to the environmental variables will also go away since each process gets its own copy. same thing applies to the cd command for directory changes. – Richard Chambers Jan 22 '15 at 16:44
  • 1
    Note that commands you used will not have any effect on subsequent execution (or any effect at all) - their intended effect disappears after system() function returns. – mvp Jan 23 '15 at 08:11
  • "I cannot see the command being run" - that's perfectly normal. If you want the command itself (as opposed to its output) to be displayed, you'll need to print it yourself. Note that the commands you've issued don't generate any output unless there is an error. – Harry Johnston Jan 23 '15 at 21:44
  • Related: https://stackoverflow.com/questions/478898/how-do-i-execute-a-command-and-get-the-output-of-the-command-within-c-using-po#comment116693291_478898 – Gabriel Staples Feb 02 '21 at 06:26

3 Answers3

9

Use popen instead of system. See example here https://msdn.microsoft.com/en-us/library/96ayss4b.aspx

char   psBuffer[128];
FILE   *pPipe;

if( (pPipe = _popen( "set PATH=%PATH%;C:/Program Files (x86)/myFolder/bin", "rt" )) == NULL )
    exit( 1 );

then

while(fgets(psBuffer, 128, pPipe)) {
    printf(psBuffer);
}

if (feof( pPipe))
    printf( "\nProcess returned %d\n", _pclose( pPipe ) );
Czipperz
  • 3,268
  • 2
  • 18
  • 25
SGrebenkin
  • 572
  • 4
  • 10
1

The output of a system call should show up on stdout.

I do not think those commands generally have any output to display if they are successful. Try adding a dir or pwd after to list the directory you are in.

If you want to get the output from the commands into the program for processing that is another issue. You will have to use os specific api, or maybe redirect the output into a file you can read.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
rileymat
  • 503
  • 4
  • 13
0

Try adding pause as below to wait after each command. On failure, error message will be displayed. On success, actual output from the command, if any, will be displayed.

system("set PATH=%PATH%;C:/Program Files (x86)/myFolder/bin & pause");
system("cd C:/thisfolder/ & pause");

Note that each call to system uses cmd.exe( as cmd /c [command] ) to execute your command and env variables like PATH in one command won't effect another.

cmd.exe /c set PATH=%PATH%;C:/Program Files (x86)/myFolder/bin 
cmd.exe /c cd C:/thisfolder/
kiranpradeep
  • 10,859
  • 4
  • 50
  • 82