How to get all of enviroment variables ?

N

Nelson

Hi people,

I know that getenv() returns the value of single environment variable,
but I need some function that returns all of env. variables
(just like when we type SET in commandline or similar).

Any help ?

Thanks
Nelson.
 
R

Rolf Magnus

Nelson said:
Hi people,

I know that getenv() returns the value of single environment variable,
but I need some function that returns all of env. variables
(just like when we type SET in commandline or similar).

Not possible in standard C++.
 
R

Rud1ger Sch1erz

Nelson said:
I know that getenv() returns the value of single environment variable,
but I need some function that returns all of env. variables
(just like when we type SET in commandline or similar).

Well, that's more a operating system question. Anyway, on a unix box
this would normally work:

<t5.cc>

#include <iostream>

extern char** environ;

int main(int argc, char** argv)
{
int i;
for(i = 0; environ != NULL; i++) {
cout << environ << endl;
}

return 0;
}

</t5.cc>

Cheers,
Rudiger
 
A

Aman

Well, that's more a operating system question. Anyway, on a unix box
this would normally work:

Another version, maybe non standard , works on both with unix/gcc4.0.2
and
windows/VC7
FYI.

#include <iostream>
using namespace std;

int main(int argc, char* argv[], char* envp[])
{
for (int i = 0; envp != 0; ++i)
cout << envp <<endl;
}

regards,
Aman.
 
G

Gavin Deane

Aman said:
Another version, maybe non standard ,

It is non-standard
works on both with unix/gcc4.0.2
and
windows/VC7

int main(int argc, char* argv[], char* envp[])

The only signatures required to be supported for main are

int main()
int main(int argc, char* argv[])

The implementation is allowed to provide
int main( /* anything else it likes */ )
as an extension.

<OT>
Of course, the signature you suggested could be a widely enough
supported extension that it is useful to the OP, given that no standard
solution exists.
</OT>

Gavin Deane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top