33
A
回答
45
NAME putenv - change or add an environment variable SYNOPSIS #include <stdlib.h> int putenv(char *string); DESCRIPTION The putenv() function adds or changes the value of environment variables. The argument string is of the form name=value. If name does not already exist in the environment, then string is added to the environment. If name does exist, then the value of name in the environment is changed to value. The string pointed to by string becomes part of the environment, so altering the string changes the environment.
在Win32这就是所谓的_putenv我相信。
另请参阅SetEnvironmentVariable如果您是长期和丑陋的函数名称的粉丝。
3
我不是积极的环境变量是你所需要的,因为它们不会在程序运行之外使用。无需使用操作系统。
你可能最好有一个singleton类或一个包含所有这些值的名称空间,并在启动程序时对它们进行初始化。
-2
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
main(int argc,char *argv[])
{
char *var,*value;
if(argc==1||argc>3)
{
fprintf(stderr,"usage:environ variables \n");
exit(0);
}
var=argv[1];
value=getenv(var);
//---------------------------------------
if(value)
{
printf("variable %s has value %s \n",var,value);
}
else
printf("variable %s has no value \n",var);
//----------------------------------------
if(argc==3)
{
char *string;
value=argv[2];
string=malloc(strlen(var)+strlen(value)+2);
if(!string)
{
fprintf(stderr,"out of memory \n");
exit(1);
}
strcpy(string,var);
strcat(string,"=");
strcat(string,value);
printf("calling putenv with: %s \n",string);
if(putenv(string)!=0)
{
fprintf(stderr,"putenv failed\n");
free(string);
exit(1);
}
value=getenv(var);
if(value)
printf("New value of %s is %s \n",var,value);
else
printf("New value of %s is null??\n",var);
}
exit(0);
}//----main
/* commands to execure on linux compile:- $gcc -o myfile myfile.c
run:- $./myfile xyz
$./myfile abc
$./myfile pqr
*/
相关问题
- 1. C设置环境变量
- 2. C#设置环境变量
- 3. 在C中设置环境变量
- 4. 在bash脚本中设置本地环境变量
- 5. 在本地bash函数变量中设置脚本的环境变量
- 6. Mono C#环境变量设置
- 7. cmake:从脚本设置环境变量
- 8. 从.ps1脚本设置环境变量
- 9. 设置环境变量
- 10. 设置Maven环境变量
- 11. 设置Java环境变量?
- 12. 设置apache环境变量
- 13. 设置环境变量
- 14. DrJava设置环境变量
- 15. 设置环境变量
- 16. 设置环境变量(kubernetes)
- 17. 设置环境变量Windows
- 18. htaccess设置环境变量
- 19. Elasticsearch设置环境变量
- 20. 设置环境变量Linux
- 21. 在javascript中设置环境变量
- 22. 在ubuntu10.4中设置环境变量
- 23. 在WCF中设置环境变量
- 24. 在git中设置环境变量
- 25. 在AWS中设置环境变量EBS
- 26. 在Cygwin中设置环境变量
- 27. 在WebStorm中设置Node.js环境变量
- 28. 在Docker中设置环境变量
- 29. 在Python中设置环境变量
- 30. 在Java Applet中设置环境变量
提问者注意 - putenv在Win32中也受支持。 – 2009-05-22 19:16:45
我们可以请使用适当的C++头名称吗?是合适的(是的,我知道......这是我的挂断)。 –
2009-05-22 19:18:11
这是C作为上帝的旨意。 – alamar 2009-05-22 19:19:05