C++ getenv() 函數
getenv() 函數是 cstdlib 頭文件的庫函數。它用於獲取環境字符串。它接受一個作為環境變量名稱的參數(取決於平台,可能區分大小寫或不區分大小寫)並返回一個 C-string,其中包含指定為參數的環境變量的值。
注意:該函數是平台相關的,如果未定義指定參數(環境變量),則返回空指針。
getenv() 函數的語法:
C++11:
char* getenv (const char* name);
參數:
name
– 表示環境變量的名稱。
返回值:
這個函數的返回類型是char*
,它返回一個 C-string,其中包含指定為參數的環境變量的值。
例:
Function call: getenv ("PATH"); Output: Specified the environment variable (PATH)
C++代碼演示getenv()函數的例子
// C++ code to demonstrate the example of
// getenv() function
#include <iostream>
#include <cstdlib>
using namespace std;
// main() section
int main()
{
char* path_string;
// getting path
path_string = getenv ("PATH");
if (path_string!=NULL)
cout<<"The current path is:"<<path_string<<endl;
return 0;
}
輸出
RUN 1:(Compiler:https://www.onlinegdb.com/ (c++)) The current path is:/opt/swift/swift-5.0-RELEASE-ubuntu14.04/usr/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin RUN 2:(Compiler:https://www.jdoodle.com/online-compiler-c++/) The current path is:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/isCOBOL2019R1/bin:/opt/cs/artifacts/Release/bin
參考:C++ getenv() 函數
相關用法
- C++ getline(string)用法及代碼示例
- C++ get_allocator()用法及代碼示例
- C++ gmtime()用法及代碼示例
- C++ unordered_map cbegin用法及代碼示例
- C++ map lower_bound()用法及代碼示例
- C++ list assign()用法及代碼示例
- C++ std::max()用法及代碼示例
- C++ std::string::push_back()用法及代碼示例
- C++ multimap key_comp()用法及代碼示例
- C++ Deque erase()用法及代碼示例
- C++ std::less_equal用法及代碼示例
- C++ set rbegin()用法及代碼示例
- C++ llround()用法及代碼示例
- C++ boost::algorithm::all_of()用法及代碼示例
- C++ string::length()用法及代碼示例
- C++ log2()用法及代碼示例
- C++ lrint() and llrint()用法及代碼示例
- C++ bitset all()用法及代碼示例
- C++ set upper_bound()用法及代碼示例
- C++ CHAR_MIN用法及代碼示例
注:本文由純淨天空篩選整理自 getenv() Function with Example in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。