chdir not working?

I have the following code:

cout << cmd;
int i;
i = chdir(&cmd[0]);
printf("%d", i);

now the cout line produce:
/home/sakuya/Documents/University/159.101/work/a6/

but the directory isnt changed in the console window... Am I doing something wrong?

thanks in advanced
LVL 10
sakuya_suAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rajeev_devinCommented:
post your complete code
sakuya_suAuthor Commented:
cmd = "/home/sakuya/Documents/University/";
            cmd += "159.101/work/";
            cmd += argv[2];
            cmd += "/";


And

if ( strcmp(argv[3], "in") == 0 )
      {
            //CD in to the directory only
            //printf("%s", run);
            cout << "-----------change Dir-------------" << endl;
            //run = &cmd[0];
            //printf("%s", run);
            int i;
            i = chdir(&cmd[0]);
            printf("%d", i);
      }
rajeev_devinCommented:
>> but the directory isnt changed in the console window
chdir changed the current directory of the process from where it is called.
Remember that shell is a different process. And you program is a different process.

Try this:
if(chdir("/home")) // It is actually changing the directory
      printf("Unable to locate the directory");
else
      system( "ls"); // This can be varified by listing the content of that directory
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

sakuya_suAuthor Commented:
ah i see whats happening now,

is there a way to change the current directory of the console window?
M_KuriachenCommented:
"
ah i see whats happening now,

is there a way to change the current directory of the console window?
"

SetCurrentDirectory() ;
rajeev_devinCommented:
>> SetCurrentDirectory() ;
First, of all SetCurrentDirectory() is a windows API. So, it cannot be used in Linux.
Second, It changes the current directory of the process from where it is called. It won't change the current directory of console.
SammoCommented:
system("cd [PATH]");

Should do it?
sakuya_suAuthor Commented:
no system("cd PATH"); does not work

to  M_, i need to the code to be able to work under Linux.

rajeev_devin, do you have any idea how to do this?

Thanks for all your help
DanRollinsCommented:
Everything I've been able to find on this issue indicates that there is no way to change the directory for the parent process (CMD.EXE or whatever Unix Shell you are using -- that's displaying the prompt in the console window).

You might need to resort to trickery... If you program is started by a batch file, then the program could write a CHDIR command to the end of the batch.  Then when it exits, the shell looks to see the next command in the batch, and executes it to change its CWD.

Or the initial batch file's final command could be to execute a batch with a specific name -- and your program rewrites that batch file during its execution.

-- Dan

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sakuya_suAuthor Commented:
heh yeah, i think i'll just create a batch with a CD command in it
rajeev_devinCommented:
>> heh yeah, i think i'll just create a batch with a CD command in it
That won't do either.
rajeev_devinCommented:
By the way in which platform you are working ?
sakuya_suAuthor Commented:
Linux
rajeev_devinCommented:
In linux can you do something like "DLL Hooking in Windows" ?
dbkrugerCommented:
The kernel call is chdir in Unix, but of course changing the child's current directory is not what you want.

The cd command in a shell doesn't launch a subprocess, it is an in-shell command that modifies the current process.
If you wrote a shell script containing:

cd foo

and ran it with:

sh myscript.sh

it STILL wouldn't work, because execution launches a subprocess, where you change the current directory, which then goes away, leaving the console unchanged.

The way to execute a shell in-process is (in bash)
. myscript.sh

The dot says "execute in this process" and the current directory is changed.
You could use exec to execute a program in process, but when the program is over, so would be the process, which is probably not what you want.

sakuya_suAuthor Commented:
i'll try the . *.sh tomorrow, thanks,

to rajeev, no, there are no dll hooks in linux
DanRollinsCommented:
It WILL work if you follow my suggestion:

Don't run your program directly from the command line.  Instead run a two-line batch file.  That batch file starts your program, waits until it is done, and then executes a second batch file (let's call it, "DoChdir.bat") .

In the program, don't run a batch file and don't do any CD comments.  Just have your program create (overwrite) the one-line file named "DoChdir.bat".  Have your program write a CHDIR command to the file.

When your program exits, the original batch file will continue running.  Its final command will be to execute "DoChdir.bat"  and the single command in "DoChdir.bat" will be to CD to the directory specified when your program wrote the "DoChdir.bat" file.

=-=-=-=-=-=-=-=
I have been assuming that your program needa to do multiple things, and needs to change the CWD for the command processsor as the final step.  HOWEVER, if *all* that your program is intended to do is to set the CWD to a particular value for the shell process, then there are other, easier, ways to do that.  Just ask.
sakuya_suAuthor Commented:
which other methods are u refering to?
DanRollinsCommented:
If all you want to do in your program is change the CWD to something specific, then there is no reason to write a C++ program to do it.  Just use a batch file (or, in Unix, a shell script).

     REM QCD.BAT (Quick Change Dir)
     REM Batch file to do everything without writing a C++ program
     REM Syntax:   QCD string
     REM Example: QCD a6
     CD /home/sakuya/Documents/University/159.101/work/%1

The above lines will save you lots of typing.  The %1 in the CD command is replaced by whatever you type at the end of the QCD command line.  You can remove the REM lines if you want.
sakuya_suAuthor Commented:
well it seems no one has produced a very useable suggestion, but the comments here got me thinking and I have then coded my program arround this area.

so I'll split point to all who made a contribution.

thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.