Blu*_*xir 7 c++ windows command-line cmd system
如何查看系统命令的输出.例如:
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;
}
Run Code Online (Sandbox Code Playgroud)
当我在Visual Studio中运行程序时,它给我一个黑屏,我看不到正在运行的命令.我需要它,所以我可以查看它是否有效.谢谢!
用popen而不是system.请参阅此处的示例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 );
Run Code Online (Sandbox Code Playgroud)
然后
while(fgets(psBuffer, 128, pPipe)) {
printf(psBuffer);
}
if (feof( pPipe))
printf( "\nProcess returned %d\n", _pclose( pPipe ) );
Run Code Online (Sandbox Code Playgroud)