Chinaunix

标题: 问个关于popen的简单问题 [打印本页]

作者: 40943460    时间: 2008-07-04 15:27
标题: 问个关于popen的简单问题
FILE *fp;

fp = popen( command ,"w");

fp 是command的输出么? 这个我能理解.

可是我看程序中有 fp 做command 的输入..

不会写这样的例子了...请教大家下.....
作者: ruchong    时间: 2008-07-04 16:05
fopen中返回的pt你就把他看作打开文件返回的文件描述符 , 他把command的结果保存到这个文件描述符中,然后调用fget()获得command的结果输出.
FILE *pt = popen("ls -l" , "r");
    if(NULL == pt)
    {
        std::cout<<"popen is faild ...."<<std::endl;
        exit (0);
    }

    char line[256];
    while(fgets(line , sizeof(line) , pt));
作者: 40943460    时间: 2008-07-04 16:08
标题: 回复 #2 ruchong 的帖子
谢谢.这个我明白了...我看到程序中有把fp当成command的输入........我有点不理解...可以这样么?
作者: yecheng_110    时间: 2008-07-04 16:10
你打开一个文件可以是只读 只写或者可读写的
popen要做的就是把 标准输入 或者 标准输出 重定向到FILE*指定的文件描述符
作者: Godbach    时间: 2008-07-04 16:33
原帖由 40943460 于 2008-7-4 16:08 发表
谢谢.这个我明白了...我看到程序中有把fp当成command的输入........我有点不理解...可以这样么?


把代码贴出来看一下吧
作者: 40943460    时间: 2008-07-04 16:35
标题: 回复 #4 yecheng_110 的帖子
还是没有懂...可能是标准输入跟标准输出没有太理解吧..能具体点举个例子么..谢谢
一楼的那个例子是标准输出定向到fp?
作者: 77h2_eleven    时间: 2008-07-04 17:00
  1.       
  2.       1 #include<stdio.h>
  3.       2 int main(void)
  4.       3 {
  5.       4     FILE * fp;
  6.       5     char buffer[80] = "12345";
  7.       6     pclose(stderr);
  8.       7     fp=popen("date", "w");
  9.       8     fputs(buffer,fp);
  10.       9     pclose(fp);
  11.      10     return 0;
  12.      11 }
复制代码

运行一下你就知道了。
作者: tian_jut    时间: 2008-07-04 17:09
原帖由 40943460 于 2008-7-4 16:35 发表
还是没有懂...可能是标准输入跟标准输出没有太理解吧..能具体点举个例子么..谢谢
一楼的那个例子是标准输出定向到fp?



man page:
  The popen() function shall execute the command specified by the string
  command. It shall create a pipe between the calling  program  and  the
  executed  command,  and shall return a pointer to a stream that can be
  used to either read from or write to the pipe.



就是说你这个命令的输出流可以用fp来访问和操作,和操作文件一样。
作者: 40943460    时间: 2008-07-07 09:33
标题: 回复 #5 Godbach 的帖子
不好意思,那天有事临时走了.关于popen还是没有弄懂 我贴下部分代码.

                 for ( j=0 ; j<user_list.user_num; j++)
                 {
                      snprintf(buffer,1024,"%s@%s",user_list.node[j].uid,domain);
                      snprintf(cmd_buf, 1024, "%s -f%s@%s %s ", SEND_MAIL_BIN, uid,
                      domain, buffer);
                      email_fp = popen(cmd_buf, "w");
                      if (NULL == email_fp)
                      {
                          print_error(ERR_UNKNOWN_ERROR, NULL,style);
                          return;
                      }
                      if ('\0' != *attach_string)
                      {
                           snprintf(att_ind_fn, 64, "att.%s", attach_string);
                           if (0 == stat(att_ind_fn, &fstat))
                           {
                                 if (0 < fstat.st_size)
                                 {
                                       listfd = open(att_ind_fn, O_RDONLY);
                                       if (-1 != listfd)
                                         {
                                            have_att = 1;
                                         }
                                 }
                           }
                      }
                      now = time(NULL);
                      init_token(mess_id);
                      fprintf(email_fp, "Message-ID: <%s@%s>\n", mess_id, domain);
                      fprintf(email_fp, "From: %s@%s\n", uid, domain);
                      fprintf(email_fp, "To: %s\n", buffer);
                      fprintf(email_fp, "Subject: %s\n", subject_str);
                      fprintf(email_fp, "X-Priority: %s\n",priority_str);
                      if(atoi(read_receipt))
                      {
                          fprintf(email_fp, "Disposition-Notification-To: %s@%s\n",uid,domain);
                      }
                      if(atoi(receipt))
                      {
                          fprintf(email_fp, "Return-Receipt-To: %s@%s\n",uid,domain);
                      }
                      rf2822_fmt_date(&now, datebuf);
                      fprintf(email_fp, "Date: %s\n", datebuf);
                      if (have_att)
                      {
                          gen_boundary(boundary);
                          fprintf(email_fp, "Content-Type: multipart/mixed;\n\tboundary=\"%s\"\n\n", boundary);
                          fprintf(email_fp, "Content-Type: text/plain;\n\n");
                          fprintf(email_fp, "This is a multi-part message in MIME format.\n\n");
                          fprintf(email_fp, "--%s\nContent-Type: text/plain;\n\n", boundary);
                      }
                      else
                      {
                        if(!strcmp(html_buf,"true"))
                        {
                           fprintf(email_fp, "--%s\nContent-Type: text/html;charset=\"gb2312\"\nContent-Transfer-Encoding: base64\n\n", boundary);
                        }
                        else
                        {
                           fprintf(email_fp, "Content-Type: text/plain;charset=\"gb2312\"\nContent-Transfer-Encoding: base64\n\n");
                        }
                      }


这个是邮件中发信的一部分程序.popen中的 cmd_buf 是qmail中的发信程序.而email_fp 这个文件描述符中的内容是信件的内容.是cmd_buf要发送的内容.这个应该算把fp当成command的输入吧?




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2