在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是正确的,这个就不是该文讨论的问题了。
相关文档:
1.求下面函数的返回值(微软)
int func(x)
{
int countx = 0;
while(x)
{
countx ++;
x = x&(x-1);
......
1.分类
内部类型 和用户定义类型
2.
整形:bool ,字符型,整形
true 1 flase 0
非零 true & ......
决定找时间把数据结构复习下,从链表开始吧。这一知识点虽说并不涉及复杂的算法,不需要费尽头脑去思考来龙去脉,但是要写出完整的程序来,还是要花一些时间的。特别是如果对指针操作不够熟练,极易出错。好久没用C写程序了……
完整代码:
/*
* 单链表基本操作C版实现程序
* 包括插入、删除、查找。
*
* ......
镜头总是被固定在标准的镜座上,镜座包围着传感器CCD,镜头与CCD受光面保持着一定的距离,使得镜头的像面与其一致,以使镜头对焦微调后成像最清晰。有几种标准的安装接口:C接口、CS接口和S接口。
所有的摄象机镜头均是螺纹接口的,CCD摄象机的镜头安装有两种工业标准,即C安装座和CS安装座。两者螺纹部分相同,但两者从镜 ......
选两个比较有代表性的函数
首先下载安装sdk,将其中的sde.dll,pe.dll和sg.dll拷贝过来
使用如下的代码,指定dll后直接调用其中的函数,
/// <summary>
/// Sets the value for a small integer column.
/// </summary>
[DllImport(".\\sde91.dll", SetLastError = true, ......