Is there any way by which I can change to any directory by executing a C program?
Well, the POSIX command for changing the current directory is:
chdir(const char*path);
See the recent POSIX documentation for chdir() is here.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
system("C:\\windows\\notepad.exe");
chdir("C:\\windows\\desktop");
return 0;
}
As per this
Yes, the chdir() function.
chdir()
changes only the current working directory of the process but not of the context in which you are working. Suppose you execute a program in the terminal and your current directory is /home/Documents
, then on executing a program having the following lines
chdir("cd ../Downloads");
will not change the working directory of the terminal, but changes that of the process only.
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.
The chdir()
function. For more info, use man chdir
.