Changing the Current Directory in Linux Using C++

Is there any way to change directory using C language?

Depending on your OS there are different calls for changing the current directory. These will normally only change the current dir of the process running the executable. After the process exits you will be in the directory you started in.

Changing the current directory in Linux using C++

int chdir(sDirectory); isn't the correct syntax to call the chdir function. It is a declaration of an int called chdir with an invalid string initializer (`sDirectory).

To call the function you just have to do:

chdir(sDirectory.c_str());

Note that chdir takes a const char*, not a std::string so you have to use .c_str().

If you want to preserve the return value you can declare an integer and use a chdir call to initialize it but you have to give the int a name:

int chdir_return_value = chdir(sDirectory.c_str());

Finally, note that in most operating system the current or working directory can only be set for the process itself and any children it creates. It (almost) never affects the process that spawned the process changing its current directory.

If you expect to find the working directory of your shell to be changed once your program terminates you are likely to be disappointed.

How can I change the shell directory in the C language program?

You cannot do that (changing your parent shell directory from inside a C program). The current directory is an attribute of every process, and your shell runs in a different process than your program (so the shell process and your program's process have each their own current directory).

Read Advanced Linux Programming. It has several chapters related to your issue.

Perhaps you might add some shell function (e.g. into your ~/.bashrc ...) which perhaps could use eval to run your C program (which would output some cd command, that the eval or source builtin of your shell would handle within the same shell process); BTW ssh-agent might be inspirational. Actually I don't recommend this route if you are a newbie.

PS. You really should motivate your question and give a lot more context; it smells badly like an XY problem.

Changing a process's current working directory programmatically

Each individual process has a notion of its "current directory". When a new process gets created, its current directory is its parent process's current directory.

A shell is just another process, no different from any other process, except that this particular process waits for you to type a command, then it executes the typed command as a new process.

It should now become obvious why there isn't any actual "cd" command. Say there was one. So, you typed the "cd" command, and the shell executes the "cd" command as a new process.

The new process changes its current directory, and exits.

So, what did you accomplish? Absolutely nothing. The shell's current directory has not changed. All that the "cd" process would do, in this hypothetical case, is change its own current directory, and nothing else. Then it terminates, and everything's back to the way it was before.

That's why "cd" is a built-in command. This command is one of several commands that's executed by the shell directly, and this command changes the shell's current directory. So, all future processes started from this shell will now have a new current directory.

The system call that changes the process's current directory is chdir(2). C, Perl, Python, and pretty much every other programming language has some function somewhere called chdir, or something similar, that executes the system call that changes the process's current directory.

Is using chdir() the only way to change the working directory in Linux C?

chdir() is the only way to change the current working directory of the process. If you are not allowed to use this function you can partially simulate chdir() by keeping track of the current directory in the application, e.g. by using a variable. Note that such a simulation will not be complete, in particular the working directory of child processes will be where your program was started, not then one in your variable.



Related Topics



Leave a reply



Submit