在C程序里和shell通信
一般我们调用shell脚本都用system()来实现,然后发现sytem返回值不好控制而且转换麻烦(还要右移4位即/256),于是我用popen来获取shell的返回值。果然在Unix世界里面,通道就是连结各个方面的桥梁啊!
代码例子如下:
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
int main (int argc, char *argv[])
{
char szline[256];
FILE *fp;
if (argc != 2)
{
printf ("usage: %s command\n", argv[0]);
return 0;
}
if ((fp = popen (argv[1], "r")) == NULL)
{
printf ("the command %s not exist\n", argv[1]);
return 0;
}
while (fgets (szline, sizeof (szline) - 1, fp) != NULL)
{
printf ("frome command:%s", szline);
}
pclose (fp);
return 0;
}
PS:奇怪的是我用如下函数:
char* get_cmd_result(char* cmd)
{
FILE *fp;
char result[256];
memset (result, 0,sizeof(result));
if((fp=popen(cmd,"r"))==NULL)
{
printf("the command %s not exist\n",cmd);
pclose(fp);
return 0;
}
while(fgets(result,sizeof(result)-1,fp)!=NULL)
{
#ifdef __DEBUG__
g_print("get result is %s\n", result);
#endif
}
pclose(fp);
return result;
}
在debian sid下就没问题,在Ubuntu10.04上调用该函数就没法返回的正确值,但在g该函数里result是正确的,这个就不是该文讨论的问题了。
相关文档:
先是内核驱动程序:
#include <linux/module.h>//具体的头文件位置为/opt/FriendlyARM/mini2440/linux-2.6.29/include/linux/*.h
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <l ......
[root@ocsser file]# cat array-05.c
//this is a program for two juzheng xiangcheng.
#include <stdio.h>
int main(){
int i,j,m,n;
int g,k,f;
int c[5][6];
& ......
<select class="wellId" id="gasOriginWellId" name="gasOriginWellId" value="${mechWellForm.gasOriginWellId}" index="true">
< ......
镜头总是被固定在标准的镜座上,镜座包围着传感器CCD,镜头与CCD受光面保持着一定的距离,使得镜头的像面与其一致,以使镜头对焦微调后成像最清晰。有几种标准的安装接口:C接口、CS接口和S接口。
所有的摄象机镜头均是螺纹接口的,CCD摄象机的镜头安装有两种工业标准,即C安装座和CS安装座。两者螺纹部分相同,但两者从镜 ......