getenv()?

N

Neelesh Bodas

Protoman said:
What does getenv() do and why would you use it?

man getenv.

It returns a pointer to the null terminated string having a value from
the environment.

#include <cstdlib>
#include <iostream>
int main()
{
std::cout << std::getenv("PWD") << std::endl;
}
 
S

S.Colancon

This function can be used for example because you implement a new algorithm
and to keep the old algo you can define an envvar that will allow you to
switch between old and new algo. By this way you define an if(getenv(...))
to determine which algo will be run.

Stef
 
N

Niklas Norrthon

Protoman said:
So, what's an enviroment variable and how do I define one?

If you don't know, you don't need to know. It's all system specific, and really has nothing
to do with C++ (except being able to access it with the getenv function).

If you really want to know read the system documentation. On Unix, FreeBSD, Linux and similar
start with the man pages for the shell you use.

/Niklas Norrthon
 
P

Protoman

For example, how would I beable to write a simple program that
determines the processor the program's executing on? Use WinXP's
enviroment variable PROCESSOR_ARCHITECTURE?
 
H

Howard

Protoman said:
For example, how would I beable to write a simple program that
determines the processor the program's executing on? Use WinXP's
enviroment variable PROCESSOR_ARCHITECTURE?

Please include the text of the message to which you're responding. (I know
that you've been told -repeatedly - how to do this.)

To anwer your question: Ask in a windows newsgroup.

-Howard
 
N

Niklas Norrthon

Protoman said:
For example, how would I beable to write a simple program that
determines the processor the program's executing on? Use WinXP's
enviroment variable PROCESSOR_ARCHITECTURE?

PROCESSOR_ARCHITECTURE is not set to anything on my system, (which
isn't windows), but here is a program that reads the value of
the environment variable PATH that usually exist both in unix
and windows environments. I'm sure you are able to modify it to
suit your needs.

#include <stdlib.h> /* getenv */
#include <stdio.h> /* printf */

int main(void)
{
const char* key = "PATH";
const char* val = getenv(key);
if (val != NULL) {
printf("%s=%s\n", key, val);
}
else {
printf("%s not set\n", key);
}
return 0;
}

Note that getenv returns a char pointer to the value of the
environment variable. You should not modify it's content, and
you must not release it's memory. If you wan't to do anything
with it you should copy it to a buffer that you have control
over.

/Niklas Norrthon
 

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

Similar Threads

ENV VAR addresses 4
Why does getenv() work backwards? 17
Use of getenv when calling C++ through JNI 1
CGI and C++ 2
setuid() and getenv()? 1
cgi post - clear data from stdin 0
getenv problem 3
Basics of C++ Exceptions 1

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top