#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main( void )
{
FILE *stream = NULL;
FILE *fp = NULL;
char buf[1024];
memset(buf, '\0', sizeof(buf) );
stream = popen( "ping -i 0.5 -c 10 www.baidu.com", "r" );
if (!stream)
{
printf("popen err\n");
return -1;
}
fp = fopen( "test_popen.txt", "w+");
if (!stream)
{
printf("fopen err\n");
return -2;
}
fread( buf, sizeof(char), sizeof(buf), stream); //将刚刚FILE* stream的数据流读取到buf中
printf("%s",buf);
fwrite(buf, 1, sizeof(buf), fp );//将buf中的数据写到FILE *wstream对应的流中,也是写到文件中
pclose( stream );
fclose( fp );
char cmd[1024];
sprintf(cmd, "date > ping.txt;%s >> %s", "ping -i 0.5 -c 10 www.baidu.com", "ping.txt");
system(cmd);
return 0;
}
Linux-ping
最新推荐文章于 2024-07-25 15:50:18 发布