在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是正确的,这个就不是该文讨论的问题了。
相关文档:
[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">
< ......
1 前言
长期以来,广大程序员为到底是使用Client/Server,还是使用Browser/Server结构争论不休,在这些争论当中,C/S结构的程序可维护性差,布置困难,升级不方便,维护成本高就是一个相当重要的因素。有很多企业用户就是因为这个原因而放弃使用C/S。然而当一个应用必须要使用C/S结构才能很好的实现其功能的时 ......
选两个比较有代表性的函数
首先下载安装sdk,将其中的sde.dll,pe.dll和sg.dll拷贝过来
使用如下的代码,指定dll后直接调用其中的函数,
/// <summary>
/// Sets the value for a small integer column.
/// </summary>
[DllImport(".\\sde91.dll", SetLastError = true, ......
51单片机 Keil C 延时程序的简单研究
by: InfiniteSpace Studio/isjfk, 1.21.2004
任何人都可以在注明原作者和出处的前提下随意转载这篇文章,但不得用于商业目的。
......