The aim is to create a gdb function that changes current path to the upper level i.e

C:\Windows to C:\

I tried to strip away characters while the buffer array != '\'

Here is what I tried:

void gdbCommand(){
    char buffer[MAXCHAR];
    strncpy(buffer,path,sizeof(buffer));
    char result[MAXCHAR];

    for(int i = 0; i < MAXCHAR;i++){
        while(buffer[i] != '\''/*is this right?*/){
            result[i] +=buffer[i];
        }
    }

    cout << result; // testing if it worked


}

Once this function is called in the main then the whole program stops. I think I'm on the wrong track or is there an easier method? Googled too many times. Could not find anything relevant. So had to come here for help/ideas.

Recommended Answers

All 4 Replies

If this is C++ why not use std::string? Something like:

#include <iostream>
#include <string>

int main () {
    std::string src, dst;
    src = "C:\\Windows";
    dst = src.substr (0, src.find_first_of ("\\") + 1);
    std::cout << "Original: " << src << std::endl;
    std::cout << "Modified: " << dst << std::endl;
    return 0;
}

Notice in the above example that to look for a backslash character you need to escape it within the string ('\\' is the backslash character, "\\" is a string that contains a backslash)

commented: Thanks +4

Or as you want to go up a level from the current path, perhaps use find_last_of:

#include <iostream>
#include <string>

int main () {
    std::string src, dst;
    src = "C:\\Windows\\subfolder";
    dst = src.substr (0, src.find_last_of ("\\") + 1);
    std::cout << "Original: " << src << std::endl;
    std::cout << "Modified: " << dst << std::endl;
    return 0;
}
commented: Works perfectly. Thanks +4

Thank you L7Sqr and JasonHippy.

find_last_of("\\") does the job :)

find_first_of("\\") does remove the part of the string but starts searching for the first backslash so it removes all subfolders after C:\.

src.substr (0, src.find_last_of ("\\") + 1); What does 0 represent? Starting position?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.