1.设置环境变量
#include <stdlib.h>
extern char **environ;
int main(void){
//获取环境变量caption的值
char *p=getenv("caption");
if(p==NULL)
printf("not found...\n");
else
printf("caption=%s\n",p);
setenv("caption","nanjing",1);
p=getenv("caption");
if(p==NULL)
printf("setenv not found...\n");
else
printf("setenv caption=%s\n",p);
//删除环境变量
unsetenv("caption");
p=getenv("caption");
if(p==NULL)
printf("unsetenv not found...\n");
else
printf("unsetenv caption=%s\n",p);
clearenv();
if(environ==NULL)
printf("clear all...\n");
return 0;
}
2.putenv
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(void){
char buf[128] = "location=shanghai";
putenv(buf);
char *p = getenv("location");
if(p == NULL){
printf("Can't find...\n");
}
else{
printf("%s\n", p);
}
strcpy(buf, "location=beijing");
p = getenv("location");
if(p == NULL){
printf("Can't find\n");
}
else
{
printf("new value: %s\n", p);
}
return 0;
}
网友评论