使用 C++ 在 Linux 中更改当前目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3747014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:13:15  来源:igfitidea点击:

Changing the current directory in Linux using C++

c++linux

提问by

I have the following code:

我有以下代码:

#include <iostream>
#include <string>
#include <unistd.h>

using namespace std;

int main()
{
    // Variables
    string sDirectory;

    // Ask the user for a directory to move into
    cout << "Please enter a directory..." << endl;
    cin >> sDirectory;
    cin.get();

    // Navigate to the directory specified by the user
    int chdir(sDirectory);

    return 0;
}

The purpose of this code is pretty self explanatory: to set a user specified directory as the current directory. My plan is to carry out operations on the files contained therein. However, when I attempt to compile this code, I receive the following error

这段代码的目的不言自明:将用户指定的目录设置为当前目录。我的计划是对其中包含的文件进行操作。但是,当我尝试编译此代码时,收到以下错误

error: cannot convert ‘std::string' to ‘int' in initialization

with reference being made to the line reading int chdir(sDirectory). I've just started programming and am only now starting to have to find out about platform specific functions, which this one is, so any help on this matter would be most appreciated.

参考线阅读int chdir(sDirectory)。我刚刚开始编程,现在才开始需要了解平台特定的功能,这是一个,所以对这个问题的任何帮助将不胜感激。

采纳答案by CB Bailey

int chdir(sDirectory);isn't the correct syntax to call the chdirfunction. It is a declaration of an intcalled chdirwith an invalid string initializer (`sDirectory).

int chdir(sDirectory);不是调用chdir函数的正确语法。它是使用无效字符串初始值设定项(`sDirectory)int调用的声明chdir

To call the function you just have to do:

要调用该函数,您只需执行以下操作:

chdir(sDirectory.c_str());

Note that chdir takes a const char*, not a std::stringso you have to use .c_str().

请注意, chdir 需要一个const char*,而不是 astd::string所以你必须使用.c_str().

If you want to preserve the return value you can declare an integer and use a chdircall to initialize it but you have to give the inta name:

如果你想保留返回值,你可以声明一个整数并使用chdir调用来初始化它,但你必须给出int一个名字:

int chdir_return_value = chdir(sDirectory.c_str());

Finally, note that in most operating system the current or working directory can only be set for the process itself and any children it creates. It (almost) never affects the process that spawned the process changing its current directory.

最后,请注意,在大多数操作系统中,只能为进程本身及其创建的任何子进程设置当前或工作目录。它(几乎)从不影响产生进程更改其当前目录的进程。

If you expect to find the working directory of your shell to be changed once your program terminates you are likely to be disappointed.

如果您希望在程序终止后找到要更改的 shell 的工作目录,您可能会感到失望。

回答by BatchyX

if (chdir(sDirectory.c_str()) == -1) {
    // handle the wonderful error by checking errno.
    // you might want to #include <cerrno> to access the errno global variable.
}

回答by MadcapLaugher

The issue is that you are string to pass an STL string to chdir(). chdir() requires a C Style string, which is just an array of characters terminated with a NUL byte.

问题是您是将 STL 字符串传递给 chdir() 的字符串。chdir() 需要一个 C 样式字符串,它只是一个以 NUL 字节结尾的字符数组。

What you need to be doing is chdir(sDirectory.c_str())which will convert it to a C Style string. And also the int on int chdir(sDirectory);isn't necessary.

您需要做的是chdir(sDirectory.c_str())将其转换为 C 样式字符串。而且 int onint chdir(sDirectory);也不是必需的。