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.
Change current working directory in child process in C
You have two mistakes,
- Usage of
scanf()
- Not waiting for child to finish its task as parent.
The following works.
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
int main()
{
int pid;
char directory[1024];
char newdirectory[1024];
pid=fork();
if(pid<0)
{
printf("\n Error ");
exit(1);
}
else if(pid==0)
{
printf("I'm child \n ");
printf(" My PID: %d \n",getpid());
getcwd(directory, sizeof(directory));
printf(" My current working directory is: %s\n", directory);
printf(" Enter the new path\n");
scanf("%1023s", newdirectory);
chdir(newdirectory);
getcwd(directory, sizeof(directory));
printf(" Path changed to: %s\n", directory);
exit(0);
}
else
{
wait(0);
printf("I'm a parent \n ");
printf("My PID is %d \n ",getpid());
printf("Bye bye \n");
exit(1);
}
}
Change the current working directory in C++
The chdir
function works on both POSIX (manpage) and Windows (called _chdir
there but an alias chdir
exists).
Both implementations return zero on success and -1 on error. As you can see in the manpage, more distinguished errno values are possible in the POSIX variant, but that shouldn't really make a difference for most use cases.
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.
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.
Related Topics
How to Read the Results of a System() Call in C++
System("Command") Produces an Error; But It Works When Invoked Directly from Bash Prompt
Qt - Updating Main Window with Second Thread
What Is the Proper Opengl Initialisation on Intel Hd 3000
Purpose of Trigraph Sequences in C++
How to Implode a Vector of Strings into a String (The Elegant Way)
"Undefined Reference To" Using 'G++' to Compile a C++ Program
Get 3D Coordinates from 2D Image Pixel If Extrinsic and Intrinsic Parameters Are Known
Std::Transform() and Toupper(), No Matching Function
String Conversion with Boost Locale: Different Behaviour on Windows and Linux
Gmon.Out Is Not Written After Compiling with Gcc -Pg -G
Why Is Std::Function Not Equality Comparable
How to Render an Opengl Frame in C++ Builder
":" (Colon) in C Struct - What Does It Mean