popen和pclose的使用 以及使用system()执行shell语句获取结果的问题

BoredNight 2011-07-19 10:39:16
我用system()执行了一些命令,然后想要获取结果。比如说这个命令的执行需要一段时间,我想获取控制台显示的内容然后在一个static text或者messagebox里面显示,提示user等待过程结束。

我在网上搜看到system可以把结果放到一个文件里面然后可以从文件里面读结果,但是这样要等到整个命令执行结束,写完文件才能读对吧?不能及时的得到控制台显示的内容。
然后我看到另外一个方法是用popen,pclose。
比如下面的代码


void executeCMD(const char *cmd, char *result)
{
char buf_ps[1024];
char ps[1024]={0};
FILE *ptr;
strcpy(ps, cmd);
if((ptr=popen(ps, "r"))!=NULL)
{
while(fgets(buf_ps, 1024, ptr)!=NULL)
{
strcat(result, buf_ps);
if(strlen(result)>1024)
break;
}
pclose(ptr);
ptr = NULL;
}
else
{
printf("popen %s error\n", ps);
}
}





但是我用的vc++6.0, 然后include了stdio.h, 但是compile的时候popen和pclose还是undeclared identifier, 还要include什么文件才能用这两个函数呢?


或者有什么更好的方法~欢迎各位大大给一点建议~~
...全文
326 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
BoredNight 2011-07-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhao4zhong1 的回复:]
Windows上是_popen
C/C++ code
Example

/* POPEN.C: This program uses _popen and _pclose to receive a
* stream of text from a system process.
*/

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

void main( void )
{

char psBuffer[128];
FILE *chkdsk;

/* Run DIR so that it writes its output to a pipe. Open this
* pipe with read text attribute so that we can read it
* like a text file.
*/
if( (chkdsk = _popen( "dir *.c /on /p", "rt" )) == NULL )
exit( 1 );

/* Read pipe until end of file. End of file indicates that
* CHKDSK closed its standard out (probably meaning it
* terminated).
*/
while( !feof( chkdsk ) )
{
if( fgets( psBuffer, 128, chkdsk ) != NULL )
printf( psBuffer );
}

/* Close pipe and print return value of CHKDSK. */
printf( "\nProcess returned %d\n", _pclose( chkdsk ) );
}

[/Quote]
我试了一下这个代码,然后把代码中的dir的command改成了我自己的代码,发现_popen的结果是null,然后进入if statement直接exit。 但是我的command用system()是可以直接运行并且结果是正确的。。。。有人知道为什么吗。。。
BoredNight 2011-07-19
  • 打赏
  • 举报
回复
囧~那个code是linux上的。。。。。。在windows上还要使用CreateProcessW函数。。。
赵4老师 2011-07-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 borednight 的回复:]
囧~那个code是linux上的。。。。。。在windows上还要使用CreateProcessW函数。。。
[/Quote]
Windows上是_popen
Example

/* POPEN.C: This program uses _popen and _pclose to receive a
* stream of text from a system process.
*/

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

void main( void )
{

char psBuffer[128];
FILE *chkdsk;

/* Run DIR so that it writes its output to a pipe. Open this
* pipe with read text attribute so that we can read it
* like a text file.
*/
if( (chkdsk = _popen( "dir *.c /on /p", "rt" )) == NULL )
exit( 1 );

/* Read pipe until end of file. End of file indicates that
* CHKDSK closed its standard out (probably meaning it
* terminated).
*/
while( !feof( chkdsk ) )
{
if( fgets( psBuffer, 128, chkdsk ) != NULL )
printf( psBuffer );
}

/* Close pipe and print return value of CHKDSK. */
printf( "\nProcess returned %d\n", _pclose( chkdsk ) );
}


BoredNight 2011-07-19
  • 打赏
  • 举报
回复
沉了。。。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧