LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2004, 01:47 AM   #1
s3b0
LQ Newbie
 
Registered: Aug 2004
Location: Poland
Posts: 2

Rep: Reputation: 0
Question environment variables in c++


hello
how can i get environment variable value in c++ program?
thanks in advance
--
s3b0
 
Old 08-26-2004, 03:38 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
I don't think there's a specific C++ interface but you can certainly use the C mechanism.

At the top of the file, add the line [code]#include <stdlib.h>[/code>

Then, to get the environment variable:
Code:
char * var = getenv("HOME");
(Replace HOME with the variable name you want; $HOME is the user's current directory.)

Hope that helps,

— Robert J. Lee
 
Old 08-26-2004, 03:47 AM   #3
s3b0
LQ Newbie
 
Registered: Aug 2004
Location: Poland
Posts: 2

Original Poster
Rep: Reputation: 0
Thumbs up

yes
that's what i was looking for
thanks

--
s3b0
 
Old 08-27-2004, 04:34 AM   #4
dileepkk
Member
 
Registered: Jul 2004
Posts: 42

Rep: Reputation: 15
If u want all the environmental variables, u can try this program

#include<unistd.h>
extern char **environ;
int
main()
{
int i;

for(i=0;environ[i]!=NULL;i++)
printf("%s\n",environ[i]);
return 0;
}

here environ is a global predefined variable

Dileep
 
Old 08-27-2004, 05:19 AM   #5
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Or even (using C++ style I/O):

Code:
#include <iostream>
int main(int,char **,char ** envp) {
for(int i=0;envp[i]!=NULL;i++)
  std::cout << envp[i];
  return 0;
}
 
Old 08-27-2004, 09:05 AM   #6
dileepkk
Member
 
Registered: Jul 2004
Posts: 42

Rep: Reputation: 15
That is true but for that we have to give parameters to main.
 
Old 08-27-2004, 09:19 AM   #7
x12344321
Member
 
Registered: Jan 2004
Distribution: Slackware 10.0|Damn Small Linux|NetBSD|Debian
Posts: 46

Rep: Reputation: 15
envp is (somewhat) depreciated. mabye not in the formal sense, but i wouldnt reccomend using it. environ is a cleaner solution.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Environment Variables - where do they go!?! iansoundz Linux - General 7 11-17-2005 02:27 AM
when to use environment variables? mark_2811 Programming 2 02-23-2004 06:09 PM
environment variables kakridge Linux - Newbie 1 07-14-2003 06:25 PM
environment variables. jISV Linux - General 0 04-05-2002 06:01 AM
environment variables aethereal Linux - Newbie 7 12-19-2001 09:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration