简体   繁体   中英

Is there any way to change directory using C language?

有没有什么方法可以通过执行C程序 更改到任何目录

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

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.

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

chdir(const char*path);

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

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.

是的, chdir()函数。

#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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM