popen()

popen()函数

 1 /*============================================
 2 > Copyright (C) 2014 All rights reserved.
 3 > FileName:my_ponen.c
 4 > author:donald
 5 > details:
 6 ==============================================*/
 7 #include <unistd.h>
 8 #include <stdio.h>
 9 #include <stdlib.h>
10 #include <string.h>
11 int main(int argc, const char *argv[])
12 {
13 int left,right,sum;
14 FILE* fp ;
15 char cmd[1024] = "";
16 while(1){
17 scanf("%d%d",&left,&right);
18 sprintf(cmd,"%s %d %d",argv[1],left,right);
19 fp = popen(cmd,"r");
20 fscanf(fp,"%d",&sum);
21 printf("result is : %d\n",sum);
22
23 pclose(fp);
24 }
25 return 0;
26 }

目标函数

 1 /*============================================
 2   > Copyright (C) 2014 All rights reserved.
 3   > FileName:mystring.c
 4   > author:donald
 5   > details:
 6 ==============================================*/
 7 #include <stdio.h>
 8 #include <stdlib.h>
 9 #include <string.h>
10 #define N 1024
11 int main(int argc, const char *argv[])
12 {
13     int index;
14     char buf[N];
15     memset(buf,0,N);
16     for(index = argc - 1;index > 0;index --){
17         strcat(buf,argv[index]);
18         strcat(buf," ");
19     }
20     printf("%s\n",buf);
21     return 0;
22 }

popen(),布布扣,bubuko.com

时间: 2024-08-09 21:20:26

popen()的相关文章

fopen、open、popen

1.fopen       --FILE* fopen(const char* path,const char* mode) 打开普通文件 带缓冲区 缓冲文件系统是借助文件结构体指针来对文件进行管理,通过文件指针对文件进行访问,既可以读写字符.字符串.格式化数据,也可以读写二进制数据 返回值: fopen()文件顺利打开后,返回指向该流的文件指针,如果打开文件失败,则返回NULL,并将错误代码存在errno中 2.open      --int open(const char* pathname

APUE之通过popen,fputc等函数获取本虚拟机网卡eth0的IP

任务:unix环境通过c程序获取本虚拟机网卡eth0的IP. 总结: 1. 标准I/O库函数相对于系统调用的函数多了个缓冲区(,buf),安全性上通过buf 防溢出. 2.用system函数输出是标准输出,进一步理解fork函数和exec函数重新开启一个进程运行程序: 3.printf 这类输出函数中" "若包含"记得要换成转义字符\"            资料链接:   http://blog.csdn.net/ce123_zhouwei/article/det

Linux的system()和popen()差异

Linux的system()和popen()差异 1. system()和popen()简介 在linux中我们可以通过system()来执行一个shell命令,popen()也是执行shell命令并且通过管道和shell命令进行通信. system().popen()给我们处理了fork.exec.waitpid等一系列的处理流程,让我们只需要关注最后的返回结果(函数的返回值)即可. 2. system().popen()源码 首先我们来看一下这两个函数在源码(伪代码)上面的差异. int s

父子进程间通信模型实现(popen)

0.FILE *popen(const char *command, const char *type); popen 函数相当于做了以下几件事: 1.创建一个无名管道文件 2. fork() 3.在子进程里, exec command 4. 在子进程里, 若 type == “r” ,  相当于进行: int fd_new = fopen("Pipe_Name",O_RDONLY); dup2(0,fd_new); 若 type == “w” ,  相当于进行: int fd_new

Python:如何得到Popen的输出?

from:http://www.cnblogs.com/bluescorpio/archive/2010/05/04/1727020.html 最近在用subprocess中的Popen做个磁盘监控小程序,但是在使用ps = Popen("df -h", shell=True, stdout=PIPE, stderr=PIPE)之后,再使用output_lines = ps.stdout.readlines()的时候,output_lines总是内容为空,有哪位知道是什么原因么? bt

Python用subprocess的Popen来调用系统命令

当我们须要调用系统的命令的时候,最先考虑的os模块.用os.system()和os.popen()来进行操作.可是这两个命令过于简单,不能完毕一些复杂的操作,如给执行的命令提供输入或者读取命令的输出,推断该命令的执行状态,管理多个命令的并行等等.这时subprocess中的Popen命令就能有效的完毕我们须要的操作.在这里对Popen予以简介. 以下是一个非常easy的样例,来自Python的官网教程:http://docs.python.org/library/subprocess.html

[py] os.system os.popen commands 执行shell

? 1.仅输出到屏幕,pwd保存的是状态<=====可用于执行shell命令 pwd=os.system(pwd) ? 2.popen可以保存命令结果 pwd=os.popen('pwd').read() ? 3,返回状态和命令结果 pwd=commands.getstatusoutput('pwd') (0, '/home/py')

python中subprocess.Popen.poll

import subprocess proc = subprocess.Popen(['python', 'test.py'], stdout=subprocess.PIPE) while 1: print proc.poll() #while 1: # print "hello" print "hello" 测试代码如上,poll函数返回码: 0 正常结束 1 sleep -15 kill None 在运行 poll的返回: A None value indica

python Popen卡死问题

程序经常卡死,定位了半天才定位到原因,原来是Popen导致的卡死: 程序如下: s = subprocess.Popen([*,*,*], stdout=subprocess.PIPE) ret = s.stdout.read() return ret 官方文档的解释是: This will deadlock when using stdout=PIPE and/or stderr=PIPE and the child process generates enough output to a p

python调用系统命令popen、system

python调用Shell脚本,有两种方法:os.system(cmd)或os.popen(cmd),前者返回值是脚本的退出状态码,后者的返回值是脚本执行过程中的输出内容.所以说一般我们认为popen更加强大 os.system(cmd): 该方法在调用完shell脚本后,返回一个16位的二进制 数,低位为杀死所调用脚本的信号号码,高位为脚本的退出状态码,即脚本中“exit 1”的代码执行后,os.system函数返回值的高位数则是1,如果低位数是0的情况下,则函数的返回值是0×100,换算为1