相关疑难解决方法(0)

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

我有以下代码:

#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).我刚刚开始编程,现在才开始寻找有关平台特定功能的问题,所以对此问题的任何帮助都会非常感激.

c++ linux

7
推荐指数
2
解决办法
1万
查看次数

为什么我们不能通过Linux中的system()系统调用来更改目录?

system ("cd ..");
Run Code Online (Sandbox Code Playgroud)

这不会产生任何错误,但也没有做任何有意义的事情.为什么?

c linux shell

1
推荐指数
2
解决办法
1818
查看次数

如何在C中更改工作目录?

chdir可用于常量字符路径(它需要a const char *),但不能用于用户输入的路径(因为它们具有类型char *).有没有办法解决?

c linux chdir

0
推荐指数
1
解决办法
2920
查看次数

标签 统计

linux ×3

c ×2

c++ ×1

chdir ×1

shell ×1