LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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-29-2009, 05:58 AM   #1
strycnine
Member
 
Registered: Jul 2006
Location: Romania
Distribution: Fedora 12
Posts: 160

Rep: Reputation: 31
change_directory


Hi, I have a problem changing the directory.
I made a program that at some point it has to change directory to, for example, /media/disk-1

I made a test with this program:

include <stdlib.h>
#include <iostream>
using namespace std;

int main(void)
{
if(setenv("PWD", "/media/disk-1", 1) != 0)
{
cerr << "eroare la modificare" << endl;
}
else
{
cout << getenv("PWD") << endl;
}

return (EXIT_SUCCESS);
}


$ ./pwd
/media/disk-1

but the directory doesn't change. Where did I do wrong?

please help

Thx
 
Old 08-29-2009, 06:08 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Changing $PWD doesn't change directory.

Edit:

It works the other way around; changing directory changes environment variable PWD.

In C++, you can change directory with the chdir() function, see this LQ thread.

Last edited by catkin; 08-29-2009 at 09:22 AM.
 
Old 08-29-2009, 06:58 AM   #3
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Rep: Reputation: 37
The problem is that $PWD is the variable that contains the CURRENT WORKING DIRECTORY, which measn that if you are in /home and you cd to $PWD what you are actually saying is cd /home... which obviously will leave you in the same place.

ex:

Quote:
[~/Desktop]$ echo $PWD
/home/raptorx/Desktop
[~/Desktop]$ cd $PWD
[~/Desktop]$ echo $?
0
[~/Desktop]$
you can see that the cd command exits with code 0, which means that it worked fine... but we stay in the same directory for obvious reasons.
 
  


Reply



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



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

All times are GMT -5. The time now is 04:24 AM.

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