I am new to programming.I want to access all the directories and sub-directories from default installed directory,but it is failing in traversing the folder, here i am passing path to constant char.Below is the code
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;
}
Use the string::c_str()
method, like stat(path.c_str())
, to convert a C++ string to a C string.
See http://cplusplus.com/reference/string/string/c_str/ for more information.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.