mercredi 30 septembre 2015

Why won't opendir() open a path after converting the path type with c_str()?

I'm trying to open a directory, the name of which (path) is currently in a std::string read in originally from a .csv file (although I don't think that changes anything about the string itself). Calling opendir(path.c_str()) returns NULL. I tried the following code, doing the conversion outside of opendir():

DIR *dir;
bool first = True;
string level = "";
struct dirent *ent;

const char * c = path.c_str();
// A
if ((dir = opendir(c)) != NULL){
    // do stuff
    // should open the directory and go here
}else{
    // always ends up here
}

While this failed with path="LeanDataBase", a directory in the project folder, substituting opendir("LeanDataBase") for opendir(c) does seem to open the directory. However, this function is recursive, so I can't hard code this value or it doesn't work and falls into an infinite loop.

I also tried printing the types, with the following two lines inserted right after "A" in the previous code:

cout << typeid(c).name() << endl;
cout << typeid("LeanDataBase").name() << endl;

Which yielded the following output:

PKc
A13_c

Does this mean that I'm passing the wrong type to opendir()? It seems like it can handle PKc, but not A13_c. Is there a way to convert the path string to the proper type?

Aucun commentaire:

Enregistrer un commentaire