I'm new in c programming and I'm asking for a simple way to change my current directory in a programm. I tried to use system("new path")
but it didn't work.
Asked
Active
Viewed 77 times
-3

moffeltje
- 4,521
- 4
- 33
- 57
-
3http://linux.die.net/man/2/chdir – stark Sep 08 '15 at 10:57
-
2How would you do it in the terminal? BTW, show your code and explain what is "_not working_" – Spikatrix Sep 08 '15 at 10:58
2 Answers
0
The reason it is not working is that system is starting a separate process, changing its working directory, then exiting. Your program needs to change its own directory by calling chdir
.

stark
- 12,615
- 3
- 33
- 50
-
i tried also chdir("new path") and it isn't working either and i'm working at windows – Stergiospaok Sp Sep 08 '15 at 11:03
-
You can't change the user's directory from a separate program. The program can only change its own directory. After it exits the user command line is still in the same dirr. – stark Sep 08 '15 at 11:06
-
-
-
-
http://stackoverflow.com/questions/2375003/how-do-i-set-the-working-directory-of-the-parent-process – stark Sep 08 '15 at 11:22
0
If you're using a unix based OS, use chdir()
. For windows you'll have to use _chdir()
, although an alias chdir exists, it is deprecated.
Both the functions return 0 on success and -1 if an error occurred.
You can only change the directory in which the program executes. You can't change the working directory of your shell.

Pooja Nilangekar
- 1,419
- 13
- 20