Is there any way by which I can change to any directory by executing a C program?
+1
A:
Well, the POSIX command for changing the current directory is:
chdir(const char*path);
Dmitry Brant
2009-08-18 13:01:09
+1
A:
#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
Kevin Boyd
2009-08-18 13:02:00
+4
A:
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.
Peter van der Heijden
2009-08-18 13:03:10
Thanks Peter, so it seems that the physical change of directory will not take place .
Biswajyoti Das
2009-08-18 13:10:23
The current directory is part of the state of a process (like open files, memory maps, environment variable...). Usually a process can't change the state of another process (usually, debugger and so on may have special privileges, but that is another story).
AProgrammer
2009-08-18 13:23:16
And this is why 'cd' is a shell builtin, not a separate executable.
Greg Rogers
2009-08-18 13:25:29