设置环境变体 [英] set environment variant

查看:53
本文介绍了设置环境变体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C\C ++设置环境变量

i使用putenv()

它可以运行但是它不起作用(环境变体没有' t更改)



我的代码

how to use C\C++ to set environment variant
i use "putenv()"
it can run but it didn't work(environment variant didn't change)

my code

int main()
{
    char a[128];
    char b[256];

    char all[512];
    cin>>a;
    cin>>b;
    cout<<'\n';
    cout<<a<<'\n';
    cout<<b<<'\n';

    strcpy(all,a);
    strcat(all,"=");
    strcat(all,b);
    cout<<all<<'\n';
    cout<<putenv(all);
}





我尝试使用函数setenv()之前但错误信息:

错误:'setenv'未在此范围内声明

我的操作系统是Windows 7.

IDE代码:: block



I try to used function "setenv()" before but the error message:
error: 'setenv' was not declared in this scope
My OS is Windows 7.
IDE Code::block

推荐答案

putenv()更改本地进程(您的程序)的环境。它不会影响系统的环境。



启动程序时,系统环境将复制到进程的本地内存中。使用 putenv()时,将更改此本地内存中的环境。此修改后的环境可以传递给由 spawn() exec()或<$ c $创建的其他进程。 c>系统()来自你的程序。但是当程序终止时,本地副本和所有修改都将丢失。
putenv() changes the environment of the local process (your program). It does not affect the environment of the system.

When a program is started, the system environment is copied to local memory of the process. When using putenv(), the environment in this local memory is changed. This modified environment can be passed to other processes created by spawn(), exec(), or system() from within your program. But when your program terminates, the local copy and all modifications are lost.


您正在做的是正确的方法,但它只适用于当前进程和可能是它的子进程。

当您检查程序外的环境变量时,它会显示旧的值。



在Windows上你可以使用 SetEnvironmentVariable [< a href =http://msdn.microsoft.com/en-us/library/windows/desktop/ms686206(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ] API。
What you're doing is the correct way to do it, but it will be applicable only to the current process and possibly its child processes.
When you check the environment variable outside your program, it will show you the old value.

On Windows you can do the same using the SetEnvironmentVariable[^] API.


这篇关于设置环境变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆