c++ 获取“错误:无法将”std::__cxx11::string* {aka std::_cxx11::basic_string < char>*}“转换为”const char*“错误,同时传递路径作为参数

wmtdaxz3  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(257)

我想从默认安装目录访问所有目录和子目录,但它在遍历文件夹时失败。这里我将路径传递给常量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;
}

字符串

rt4zxlrg

rt4zxlrg1#

使用string::c_str()方法,如stat(path.c_str()),将C++字符串转换为C字符串。
更多信息请参见 std::string::c_str

相关问题