-3

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.

moffeltje
  • 4,521
  • 4
  • 33
  • 57

2 Answers2

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
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