首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在将路径作为参数传递时,出现" error : cannot convert 'std::__cxx11::string* { as std::__cxx11::basic_string<char>*}‘to 'const char*’错误

问在将路径作为参数传递时,出现" error : cannot convert 'std::__cxx11::string* { as std::__cxx11::basic_string<char>*}‘to 'const char*’错误
EN

Stack Overflow用户
提问于 2020-04-09 03:02:45
回答 1查看 3.1K关注 0票数 2

我是编程新手。我想访问默认安装目录中的所有目录和子目录,但无法遍历文件夹,这里我将路径传递给常量char.Below是代码

代码语言:javascript
复制
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;
}
EN

Stack Overflow用户

回答已采纳

发布于 2020-04-09 07:24:15

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

有关详细信息,请参阅http://cplusplus.com/reference/string/string/c_str/。

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61108104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档