Is there any way by which I can change to any directory by executing a C program?
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
.
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.
Well, the POSIX command for changing the current directory is:
chdir(const char*path);
Yes, the chdir() function.
#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
来源:https://stackoverflow.com/questions/1293660/is-there-any-way-to-change-directory-using-c-language