Chinaunix首页 | 论坛 | 博客
  • 博客访问: 556315
  • 博文数量: 127
  • 博客积分: 1169
  • 博客等级: 少尉
  • 技术积分: 1298
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-16 14:29
个人简介

空白

文章分类

全部博文(127)

分类: LINUX

2012-09-18 23:46:32


  1. /**
  2.  *
  3.  * This is a simple program to test the popen function
  4.  * create time: 2012-09-18
  5.  *
  6.  **/

  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>

  12. #define MAX_LEN 1024

  13. int main(int argc, char **argv)
  14. {
  15.     do
  16.     {
  17.         if(2 != argc)
  18.         {
  19.             printf("Usage: %s outputfile!\r\n", argv[0]);
  20.             break;
  21.         }

  22.         FILE *s;
  23.         FILE *wstream;
  24.         char buf[MAX_LEN];
  25.         char cmd[MAX_LEN];

  26.         memset(cmd, 0, sizeof(cmd));

  27.         strcpy(cmd, "ifconfig -a");

  28.         memset(buf, 0, sizeof(buf));
  29.     
  30.         printf("buf %s\r\n", buf);
  31.     
  32.         s = popen(cmd, "r"); /* 读取cmd命令执行结果输出到s中 */

  33.         wstream = fopen(argv[1], "w+");/* 以写方式新建立一个文件,保存cmd执行输出 */
  34.         
  35.         fread(buf, sizeof(char), sizeof(buf), s); /* 读取cmd执行结果到buf */

  36.         fwrite(buf, 1, sizeof(buf), wstream); /* 将buf中数据写入文件 */

  37.         pclose(s);

  38.         fclose(wstream);

  39.     } while(0);

  40.     return 0;
  41. }

阅读(1270) | 评论(0) | 转发(0) |
0

上一篇:linux find

下一篇:A simple test code

给主人留下些什么吧!~~