|
|
发表于 2006-2-25 07:41:21
|
显示全部楼层
你写的没有错。只是你忽略了一点。当你在ternimal下运行一个程式时,这个程式复制了所有当前这个ternimal的环境变量。然后这个程式就成了一个单独的个体。当你要改变这个程式的环境变量,这些环境变量并不会影响到那个terninal。 我不到我解释的是不是很清楚。我改了你的程式,运行一下就知道为什么了。
#include <stdlib.h>
#include <stdio.h>
int main() {
static char envbuf[256];
char readenv[100];
sprintf(envbuf,"MYVAR=%s","MYVAL");
if(putenv(envbuf)) {
printf("Sorry, putenv() couldn't find the memory for %s\n",envbuf);
/* Might exit() or something here if you can't live without it */
}
strcpy(readenv, getenv("MYVAR"));
printf("readenv: %s\n", readenv);
exit (0);
} |
|