* }' to ' const char *'错误-6ren"> * }' to ' const char *'错误-我是编程新手。我想从默认安装目录访问所有目录和子目录,但是遍历该文件夹失败,这里我将路径传递给常量char。下面是代码 using namespace std; int reading(const c-6ren">
gpt4 book ai didi

c++ - 在将路径作为参数传递时获取 "error: cannot convert ' std::__ cxx11::string * {aka std::__ cxx11::basic_string * }' to ' const char *'错误

转载 作者:行者123 更新时间:2023-12-02 10:18:44 24 4
gpt4 key购买 nike

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

最佳答案

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

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

关于c++ - 在将路径作为参数传递时获取 "error: cannot convert ' std::__ cxx11::string * {aka std::__ cxx11::basic_string <char> * }' to ' const char *'错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61108104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com