Reputation: 37
I want to access all the directories and subdirectories from the default installed directory, but it is failing in traversing the folder. Here I am passing the path to a 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;
}
Upvotes: 2
Views: 4841
Reputation: 2057
Use the string::c_str()
method, like stat(path.c_str())
, to convert a C++ string to a C string.
See std::string::c_str for more information.
Upvotes: 2