July 10th, 2003, 04:56 PM
recursive function string problem
I have a recursive function that search the directory starting from a point that I pass it. The prototype is something like:
void recursive(char *);
I have a class that I use for the settings of the program. The class has many accessor functions, two of which are like this.
void CONFIG::setRoot(char * path) // sets the root path
char * CONFIG::getRoot() // gets the root path
I have a function populate the settings class . So in my main I do something like this:
The function populates the settings properly because if I do this:
it prints: c:\test
This is the problem. If I call the recursive function like this
it will do it's recursive thing propertly. However, if I do it like this
it wont work. Since getRoot is set to return a character pointer, shouldn't this function work with no problem?
what am I doing wrong. :(
July 10th, 2003, 06:36 PM
your returning a reference to a private data member, maybe that causes problems? what if instead you pass getRoot a dynamically allocated buffer and copy the the path into that, that way your following the data encapsulation rules.
July 10th, 2003, 06:39 PM
Ahem, shouldn't that be:
July 10th, 2003, 07:29 PM
Sorry, that's a mistake of the posting, not the source code. I certainly have it that way program code.
I should clarify that the function works. However it doesn't do the recursive thingy to were I point it to but rather where the binary itself is located. If I pass it a string literal then it works correctly.
So if the binay is located at:
and I pass it a string literal
it will recursively look in that folder correctly however if I call it like this
it will look under:
July 10th, 2003, 10:02 PM
basically, when the line was read from the text file, if the string had a new line character right after it, the setRoot() would copy the new line character too.
if the line had a space, it would only copy up to, but not including, the space. So what I'm doing is if the line has a \n right after it like this:
I'm overwritting that last line with the null terminator.
printf("found NULL at end\n");
path[strlen(path) - 1] = '\0';
now the function works properly. Of course, I'm gonna put the code up top where it belongs, in the setRoot() functions.