atoi

在用脚本打开关闭可执行程序时,看到此函数:

上网查了查,记录如下:

字符串转换成整型数。ASCII to integer 的缩写。

原型:int atoi(const char *nptr);

参数nptr字符串,如果第一个非空格字符存在,是数字或者正负号则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数。否则,返回零。

头文件: #include <stdlib.h>

程序例:

1)

#include<stdlib.h>

#include<stdio.h>

intmain(void)

{

floatn;

char*str="12345.67";

n=atoi(str);

printf("string=%sinteger=%f\n",str,n);

return0;

}


执行结果:

string = 12345.67 integer = 12345.000000

2)

#include<stdlib.h>
#include<stdio.h>
intmain()
{
chara[]="-100";
charb[]="123";
int c;
  c=atoi(a)+atoi(b);
printf("c=%d\n",c);
return0;
}

执行结果:

c = 23

源代码:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>  
#include <stdio.h>  
#include <stdlib.h>  

int main()
{
FILE *pstr; 
char cmd[128],buff[512];
pid_t pID,pid;
int pidnum;
char *name= "Test";/*进程名*/
char *p = NULL;
int ret=3;
memset(cmd,0,sizeof(cmd));
sprintf(cmd, "ps -ef|grep %s",name); //字符串格式化命令,主要功能是把格式化的数据写入某个字符串中
pstr=popen(cmd, "r");
if(pstr==NULL)
{ return 1; }
memset(buff,0,sizeof(buff));
fgets(buff,512,pstr);
printf("%s\n",buff); //
p=strtok(buff, " ");printf( "p is:%s\n",p);
p=strtok(NULL, " "); printf( "p is:%s\n",p);
pclose(pstr); //这句是否去掉,取决于当前系统中ps后,进程ID号是否是第一个字段//关闭由popen打开的管道
if(p==NULL)
{ return 1; }
//printf( "pid:%s\n",p);
if(strlen(p)==0)
{ return 1; }
if((pidnum=atoi(p))==0)
{ return 1; }
printf("pidnum: %d\n",pidnum); //
pID=(pid_t)pidnum;
ret=kill(pID,0);
printf("ret= %d \n",ret); //
if(0==ret)
{
printf("process: %s exist!\n",name); //
}
else 
{
printf("process: %s not exist!\n",name); //
pstr=popen("~/yang/co_work/test.sh","r");
fgets(buff,sizeof(buff),pstr);
printf("%s",buff);
pclose(pstr);
//system("~/yang/co_work/test.sh");
}
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值