#1
  1. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13

    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:

    CONFIG settings;
    function_that_populates_settings(settings);

    The function populates the settings properly because if I do this:
    printf("%s", settings.getRoot());

    it prints: c:\test

    This is the problem. If I call the recursive function like this
    recursive("c:\test");
    it will do it's recursive thing propertly. However, if I do it like this
    recursive(settings.getRoot);
    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. :(
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    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.
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247
    Ahem, shouldn't that be:
    recursive(settings.getRoot());
  6. #4
  7. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13
    Ahem, shouldn't that be:
    recursive(settings.getRoot());
    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:
    c:\foo\bar\app.exe
    and I pass it a string literal
    recursive("c:\\test");
    it will recursively look in that folder correctly however if I call it like this
    recursive(settings.getRoot());
    it will look under:
    c:\foo\bar\app.ext
  8. #5
  9. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13
    FIXED! :D

    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:
    c:\test\n
    I'm overwritting that last line with the null terminator.

    char path[260];
    strcpy(path, settings.getRoot());

    if(strchr(path,'\n')){
    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.

IMN logo majestic logo threadwatch logo seochat tools logo