why when i try system("cd PATH"); , terminal can't go to my path

i have a trouble with :

system("cd mypath");

when i try this in C programming language terminal doesn't do anything. i need help.

Upvotes: 0

Views: 72

Answers (2)

ENodder
ENodder

Reputation: 16

look just do

chdir("path");

or

system("chdir PATH"); //linux

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409404

The system function creates a whole new process, separate from the one calling the function.

Each process have its own current working directory associated with it, and this working directory is specific to that process only. Changing the working directory of one process will not change it for another process.

If you want to change the working directory of your own process use operating-system specific functions to to it. Like chdir on Linux (and other POSIX system like macOS), or SetCurrentDirectory in Windows.


Note that if you change directory in your own process, the directory of the shell or console that invoked your program will not be changed, as it's also a separate process from yours.

Upvotes: 10

Related Questions