0

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?

Jangell
  • 23
  • 7
  • 2
    And the relevant information: what is the *(textual) value* of `c` at time of invocation? Since specifying the string works (with a known value), and `c` is of the right type, then the likely culprit is the actual value supplied is .. unexpected/different. – user2864740 Sep 30 '15 at 21:01
  • 4
    Psychic prediction: The failing string includes a newline and/or carriage return that was in the file it was read from and wasn't stripped. – ShadowRanger Sep 30 '15 at 21:04
  • @ShadowRanger, let's see who's prediction is correct... I have the feeling it is yours :) – SergeyA Sep 30 '15 at 21:05
  • And to go with these psychic predictions, some debugging/troubleshooting tips: 1) just before the `opendir()` call add something like `printf("c is \"%s\"\n", c);`. Note the quotes around the dump of the string so that whitespace can be easily detected. 2) post actual code that reproduces your problem so things like `path` going out of scope or being changed aren't hidden. and 3) dump `errno` and/or use `perror()` to tell you what the specific error is. – Michael Burr Sep 30 '15 at 21:40
  • @ShadowRanger, you got it! I used [this](http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring) example of how to trim whitespace and I got it to work. Thank you! – Jangell Sep 30 '15 at 22:53

1 Answers1

4

Looking at my crystall ball, I see the following issue: path is modified (or even leaves the scope) after path.c_str() is called, but before opendir() is called. It is usually a bad practice to remember result of c_str() in any variable, as it leads to issues like this. c_str() is intended for in-place usage, like following

opendir(path.c_str());
SergeyA
  • 61,605
  • 5
  • 78
  • 137