Is there any way to change directory using C language?

后端 未结 6 1789
无人及你
无人及你 2020-11-27 08:24

Is there any way by which I can change to any directory by executing a C program?

相关标签:
6条回答
  • 2020-11-27 08:53

    Well, the POSIX command for changing the current directory is:

    chdir(const char*path);
    

    See the recent POSIX documentation for chdir() is here.

    0 讨论(0)
  • 2020-11-27 08:53
    #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

    0 讨论(0)
  • 2020-11-27 08:57

    Yes, the chdir() function.

    0 讨论(0)
  • 2020-11-27 08:58

    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.

    0 讨论(0)
  • 2020-11-27 09:05

    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.

    0 讨论(0)
  • 2020-11-27 09:11

    The chdir() function. For more info, use man chdir.

    0 讨论(0)
提交回复
热议问题