system (\"cd ..\");
This doesn\'t produce any error but also doesn\'t do anything meaningful. Why?
That's because it executes another shell. Inside this shell, the working directory changes, but that has no effect on the calling program.
system
runs the command you passed it in a different process (in a subshell). That subshell changes directories and promptly exits.
That's all perfectly valid, but perfectly useless. Use chdir
to change your working directory.
The system()
function makes a fork()
that creates a process being a copy of the initial one.
The current directory depends on the environment of a process (it is stored within the environment variables of a process). Thus when the child process, having its own environment, makes a cd
, that affects only the child process, not the parent.
Parent process: in /home/x/y
Child process (after the fork): in /home/x/y
Doing a cd ..
in the child process sets its local environment to /home/x
But the parent process is still in /home/x/y
Parent process waits for child to complete the system
call, then continue its own execution having its own environment (current directory) unchanged.