在 C++ 中访问环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/631664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing Environment Variables In C++
提问by James Thompson
I'd like to have access to the $HOME
environment variable in a C++ program that I'm writing. If I were writing code in C, I'd just use the getenv()
function, but I was wondering if there was a better way to do it. Here's the code that I have so far:
我想访问$HOME
我正在编写的 C++ 程序中的环境变量。如果我用 C 编写代码,我只会使用该getenv()
函数,但我想知道是否有更好的方法来做到这一点。这是我到目前为止的代码:
std::string get_env_var( std::string const & key ) {
char * val;
val = getenv( key.c_str() );
std::string retval = "";
if (val != NULL) {
retval = val;
}
return retval;
}
Should I use getenv()
to access environment variables in C++? Are there any problems that I'm likely to run into that I can avoid with a little bit of knowledge?
我应该使用getenv()
C++ 访问环境变量吗?有什么我可能会遇到的问题,我可以通过一点点知识来避免吗?
采纳答案by Matt Davis
There is nothing wrong with using getenv()
in C++. It is defined by stdlib.h
, or if you prefer the standard library implementation, you can include cstdlib
and access the function via the std::
namespace (i.e., std::getenv()
). Absolutely nothing wrong with this. In fact, if you are concerned about portability, either of these two versions is preferred.
getenv()
在 C++ 中使用没有任何问题。它由 定义stdlib.h
,或者如果您更喜欢标准库实现,您可以cstdlib
通过std::
命名空间(即std::getenv()
)包含和访问该函数。这绝对没有错。事实上,如果你关心便携性,这两个版本中的任何一个都是首选。
If you are notconcerned about portability and you are using managed C++, you can use the .NET equivalent - System::Environment::GetEnvironmentVariable()
. If you want the non-.NET equivalent for Windows, you can simply use the GetEnvironmentVariable()
Win32 function.
如果您不关心可移植性并且您使用的是托管 C++,则可以使用 .NET 等效项 - System::Environment::GetEnvironmentVariable()
. 如果您想要 Windows 的非 .NET 等效项,您可以简单地使用GetEnvironmentVariable()
Win32 函数。
回答by Vlad
I would just refactor the code a little bit:
我只是稍微重构一下代码:
std::string getEnvVar( std::string const & key ) const
{
char * val = getenv( key.c_str() );
return val == NULL ? std::string("") : std::string(val);
}
回答by Brian R. Bondy
- If you are on Windows you can use the Win32 API GetEnvironmentVariable
- On other linux/unix based systems use getenv
- 如果您使用的是 Windows,则可以使用 Win32 API GetEnvironmentVariable
- 在其他基于 linux/unix 的系统上使用getenv
Why use GetEnvironmentVariable in Windows, from MSDN getenv:
为什么在 Windows 中使用 GetEnvironmentVariable,来自 MSDN getenv:
getenv operates only on the data structures accessible to the run-time library and not on the environment "segment" created for the process by the operating system. Therefore, programs that use the envp argument to main or wmain may retrieve invalid information.
getenv 仅对运行时库可访问的数据结构进行操作,而不对操作系统为进程创建的环境“段”进行操作。因此,对 main 或 wmain 使用 envp 参数的程序可能会检索到无效信息。
This function can retrieve either a system environment variable or a user environment variable.
此函数可以检索系统环境变量或用户环境变量。
回答by Mykola Golubyev
In c++ you have to use std::getenv and #include <cstdlib>
在 C++ 中,你必须使用 std::getenv 和 #include <cstdlib>