我有以下代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
此代码的目的非常简单:将用户指定的目录设置为当前目录.我的计划是对其中包含的文件进行操作.但是,当我尝试编译此代码时,我收到以下错误
error: cannot convert ‘std::string’ to ‘int’ in initialization
Run Code Online (Sandbox Code Playgroud)
参考线读数int chdir(sDirectory).我刚刚开始编程,现在才开始寻找有关平台特定功能的问题,所以对此问题的任何帮助都会非常感激.
chdir可用于常量字符路径(它需要a const char *),但不能用于用户输入的路径(因为它们具有类型char *).有没有办法解决?