【问题标题】:Changing the directory by using the path specified (path includes filename as a string)使用指定的路径更改目录(路径包括文件名作为字符串)
【发布时间】:2019-10-25 21:48:08
【问题描述】:

我想切换到路径指定的目录。文件名是一个字符串,包含在路径中。

我已将文件名存储在一个字符串中,并将此文件名添加到路径中。接下来使用chdir()我正在切换到路径指定的目录。

我知道文件名可以在路径本身中指定,但我希望它这样做,因为文件名会不断变化。

int main()
{
    system("echo -n '1. Current Directory is '; pwd");

    std::string filename ("1184581000.pcd");

    cout<<filename;

    chdir("/home/usrn/pcd/"+ filename"");

    system("echo -n '1. Current Directory is '; pwd");

    return 0;  
}

结果是:

error: expected ‘)’ before string constant
 chdir("/home/usrn/pcd/"+ ls"");

error: cannot convert ‘std::__cxx11::basic_string<char>’ to ‘const char*’ for argument ‘1’ to ‘int chdir(const char*)’
chdir("/home/usrn/pcd/"+ ls"");

【问题讨论】:

标签: c++ linux string variables


【解决方案1】:

语法应该是:

chdir(("/home/usrn/pcd/"+ filename).c_str());

【讨论】:

猜你喜欢
  • 2020-04-14
  • 1970-01-01
  • 1970-01-01
  • 2013-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-05
  • 2019-07-17
相关资源
最近更新 更多