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)的行。我刚刚开始编程,现在才开始了解平台特定的功能,这一点,所以对此问题的任何帮助都将非常感激。
8
int chdir(sDirectory);
isn't the correct syntax to call the chdir
function. It is a declaration of an int
called chdir
with an invalid string initializer (`sDirectory).
int chdir(sDirectory);调用chdir函数不是正确的语法。它是一个名为chdir的int的声明,它带有一个无效的字符串初始值设定项(`sDirectory)。
To call the function you just have to do:
要调用该函数,您只需执行以下操作:
chdir(sDirectory.c_str());
Note that chdir takes a const char*
, not a std::string
so you have to use .c_str()
.
请注意,chdir采用const char *,而不是std :: string,因此必须使用.c_str()。
If you want to preserve the return value you can declare an integer and use a chdir
call to initialize it but you have to give the int
a 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的工作目录,则可能会感到失望。
5
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.
}
2
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 Style字符串,它只是一个以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 Style字符串。还有int chdir(sDirectory)上的int;没有必要。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2010/09/19/62b077020a7fb2aa121eb1fc3d72f726.html。