Solved

referencing an environment variable in .cc code

Posted on 2004-10-11
5
225 Views
Last Modified: 2010-04-01
How can we reference or set an environment variable inside a .cc file?
Do we define them as macros?
How can it be defined. Please provide an example.
Is there an alternative?

Thanks
0
Comment
Question by:dkamdar
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 12278081
You can always obtain the *value* of an environment variable at runtime using 'getenv()', e.g.

/* GETENV.C: This program uses getenv to retrieve
 * the LIB environment variable and then uses
 * _putenv to change it to a new value.
 */

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

void main( void )
{
   char *libvar;

   /* Get the value of the LIB environment variable. */
   libvar = getenv( "LIB" );

   if( libvar != NULL )
      printf( "Original LIB variable is: %s\n", libvar );
}
0
 

Author Comment

by:dkamdar
ID: 12278340
Hey jkr,

Thanks for the reply.
What is that _ before putenv for?
Beacause I saw my code uses putenv().
Can you please explain!

0
 
LVL 86

Expert Comment

by:jkr
ID: 12278356
>>What is that _ before putenv for?

Ask MS :o)

Seriously, they tend to prepend underscores to (most) POSIX-compliant functions for an presumeably unknown reason...
0
 
LVL 3

Expert Comment

by:aravindtj
ID: 12278481
just a small modification for Macro....

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

#define ENVIRON getenv("PATH")
#define ENV_VAL(x) getenv(x)
int main()
{
 printf("Environment Path variable = %s", ENVIRON);
 printf("Environment Prompt Value = %s", ENV_VAL("PROMPT"));
return 0;
}

0
 

Author Comment

by:dkamdar
ID: 12278572
but hey ... I never told I am using MS?
I am on solaris machine.
Anyways i guess it just depends from copiler to compiler as far as naming the system functions go!

Thnaks arvind for that ... but i dont think I would want to compilicate it that way.

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question