viewing output of system() call in C++
Asked Answered
W

4

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!

Willdon answered 22/1, 2015 at 15:48 Comment(7)
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").Stencil
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).Ermeena
Did you consider using some OS API (e.g. _chdir...) instead of calling system ? Or use some framework like POCO, Qt, or perhaps Boost.Ermeena
@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.Stencil
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.Vantassel
"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.Herewith
Related: #479398Lease
A
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 ) );
Anguilla answered 22/1, 2015 at 16:22 Comment(0)
T
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.

Thamora answered 22/1, 2015 at 15:59 Comment(1)
I backquoted "system" since a system call is something very different of system library functionErmeena
H
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/
Heal answered 22/1, 2015 at 17:27 Comment(0)
B
0

As @SGrebenkin mentioned, system should redirect any output of the commands to stdout, unless they are redirected to somewhere else.

While the accepted answer suggested popen, it is worth mentioning that popen on windows might have unwanted side effects, especially for GUI applications.

If used in a Windows program, the _popen function returns an invalid file pointer that causes the program to stop responding indefinitely. _popen works properly in a console application.

There's a library that can capture the output of a command, and can then be output to stdout with printf.

https://github.com/Neko-Box-Coder/System2

It works for posix and windows systems. (It doesn't use popen for windows so it should also work in GUI applications)

It can send input and receive output from command.

And have blocking and non-blocking version.

And it has both header only and source version as well

Borchert answered 25/2, 2024 at 17:12 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.