简体   繁体   English

有没有办法用C语言更改目录?

[英]Is there any way to change directory using C language?

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

The chdir() function. chdir()函数。 For more info, use man chdir . 有关更多信息,请使用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: 那么,用于更改当前目录的POSIX命令是:

chdir(const char*path);

See the recent POSIX documentation for chdir() is here . 有关chdir()最新POSIX文档,请参阅此处

chdir() changes only the current working directory of the process but not of the context in which you are working. chdir()仅更改进程的当前工作目录,但不更改您正在使用的上下文。 Suppose you execute a program in the terminal and your current directory is /home/Documents , then on executing a program having the following lines 假设您在终端中执行程序,当前目录是/home/Documents ,然后执行具有以下行的程序

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 按此

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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