在线文档教程
C++
应用 | Utilities

std::getenv

性病:getenv

Defined in header
char* getenv( const char* env_var

搜索环境清单主机环境%28提供OS%29,用于与env_var并返回指向与匹配的环境列表成员关联的C字符串的指针。

This function is not required to be thread-safe. Another call to getenv, as well as a call to the POSIX functions setenv(), unsetenv(), and putenv() may invalidate the pointer returned by a previous call or modify the string obtained from a previous call.(until C++11)
This function is thread-safe (calling it from multiple threads does not introduce a data race) as long as no other function modifies the host environment. In particular, the POSIX functions setenv(), unsetenv(), and putenv() would introduce a data race if called without synchronization.(since C++11)

修改由getenv调用未定义的行为。

参数

env_var-null-terminated character string identifying the name of the environmental variable to look for

返回值

字符串标识环境变量的值,如果找不到该变量,则为空指针。

注记

关于POSIX系统,环境变量也可以通过全局变量访问。environ,宣布为extern char **environ;在<unistd.h>,通过第三个可选的论点,envp...的主要功能...

二次

#include <iostream> #include <cstdlib> int main() { if(const char* env_p = std::getenv("PATH")) std::cout << "Your PATH is: " << env_p << '\n'; }

二次

可能的产出:

二次

Your PATH is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

二次

另见

c getenv文件

*。

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/实用程序/getenv