如何列出所有的环境变量在C / C ++应用程序 [英] How to list all environment variables in a c/c++ app

查看:313
本文介绍了如何列出所有的环境变量在C / C ++应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,在C编程++时,我可以访问与 GETENV

I know that when programming in c++ I can access individual environment variables with getenv.

我也知道,在OS X的终端,我可以列出所有使用 <当前环境变量code> ENV

I also know that, in the os x terminal, I can list ALL of the current environment variables using env.

我感兴趣的是获得的环境变量是提供给我跑步C ++程序的完整列表。是否有一个C / C ++函数,将列出他们吗?换句话说,有没有办法来调用 ENV 从我的C ++ code?

I'm interested in getting a complete list of the environment variables that are available to my running c++ program. Is there a c/c++ function that will list them? In other words, is there a way to call env from my c++ code?

推荐答案

使用 ENVIRON 全局变量。这是一个空结尾的指向字符串格式的数组的name = value 。这里的 ENV 的缩影克隆:

Use the environ global variable. It is a null-terminated pointer to an array of strings in the format name=value. Here's a miniature clone of env:

#include <stdlib.h>
#include <stdio.h>

extern char **environ;

int main(int argc, char **argv) {
    for(char **current = environ; *current; current++) {
        puts(*current);
    }
    return EXIT_SUCCESS;
}

这篇关于如何列出所有的环境变量在C / C ++应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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