【问题标题】:Getting "error: cannot convert 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' to 'const char*' error while passing a path as argument将路径作为参数传递时出现“错误:无法将 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' 转换为 'const char*' 错误
【发布时间】:2020-04-08 19:02:44
【问题描述】:

我是编程新手。我想从默认安装目录访问所有目录和子目录,但遍历文件夹失败,这里我将路径传递给常量 char。下面是代码

using namespace std;


int reading(const char *d_path)
{
    cout<<"In Reading"<<endl;

    /*bfs::path pathSource("c:\\Program Files\\");*/
    struct stat info; //

    DIR *dir;
    struct dirent *ent;
    dir= opendir (d_path);
    cout<<dir<<endl;
  if ((dir = opendir (d_path)) != NULL)
  {
      cout<<"in IF"<<endl;
       while ((ent = readdir (dir)) != NULL)
       {
          if (ent->d_name[0] != NULL)
          {
              cout<<"New"<<endl;
              string path = string (d_path) + string(ent->d_name) + '\\' ;
              cout<< "Entry = "<<path<<endl;
              stat (path,&info);
              if(S_ISDIR(info.st_mode))
              {
                  reading(path);
              }


          }
       }
       closedir (dir);
  }
  /* print all the files and directories within directory */

 else
    {
  /* could not open directory */
      perror ("");

    }

return 0;
}

【问题讨论】:

标签: c++


【解决方案1】:

使用string::c_str() 方法,如stat(path.c_str()),将C++ 字符串转换为C 字符串。

更多信息请参见http://cplusplus.com/reference/string/string/c_str/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多