Solved

referencing an environment variable in .cc code

Posted on 2004-10-11
5
222 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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now