在C ++中查看system()调用的输出 [英] viewing output of system() call in C++
本文介绍了在C ++中查看system()调用的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何查看系统命令的输出。例如:
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;
}
当我在Visual Studio中运行程序时,黑屏,我看不到正在运行的命令。我需要它,所以我可以查看它是否工作。感谢!
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!
推荐答案
使用 popen
系统。请参见此处的示例 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 );
然后
while(fgets(psBuffer, 128, pPipe)) {
printf(psBuffer);
}
if (feof( pPipe))
printf( "\nProcess returned %d\n", _pclose( pPipe ) );
这篇关于在C ++中查看system()调用的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文