-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,264
  • 4
  • 29
  • 52

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
  • 11,447
  • 3
  • 32
  • 46
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,303
  • 10
  • 19