Results 1 to 6 of 6
What is the reason for the "cd" command not working through "system".. In example system("cd") does not execute. Or maybe there is a trick to making it work....
THaNx...
- 09-03-2007 #1Just Joined!
- Join Date
- Sep 2007
- Posts
- 3
why System("cd") doesn't work?
What is the reason for the "cd" command not working through "system".. In example system("cd") does not execute. Or maybe there is a trick to making it work....
THaNx
- 09-03-2007 #2
What makes you think it doesn't execute? You might want to try the full path.
- 09-03-2007 #3
I don't think full path actually makes any difference, should work. Example:
That works fine.Code:#include <stdio.h> #include <stdlib.h> int main() { system("ls"); return(0); }
- 09-03-2007 #4Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
A system command runs in a sub-shell, so a "cd" in your program changes the working directory of the sub-shell, not its parent, the shell from which the sub-shell was launched.
When the command exits, you're back in the original directory because the current shell has not done a "cd".
Use the chdir() function.
Regards
- 09-21-2007 #5Just Joined!
- Join Date
- Sep 2007
- Posts
- 10
- 09-21-2007 #6
Franklin explained why this doesn't work ^^ See above.


Reply With Quote
