ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

So after looking around I finally found an easy solution. Just execute the command "rospack find package_name" from your program and get the output of the command back as string. Then just append the name of your file contained in that folder. Of course here I suppose that the name of the package is known and that it will not change. I think this is goes straight forward in Python. In C++ It can be implemented this way:

string get_package_path(char const* package_name) {
    stringstream ss;
    ss << "rospack find " << package_name;
    FILE* pipe = popen(ss.str().c_str(), "r");
    if (!pipe) return "ERROR";
    char buffer[128];
    string result = "";
    while(!feof(pipe)) {
        if(fgets(buffer, 128, pipe) != NULL)
            result += buffer;
    }
    pclose(pipe);
    return result.substr(0, result.length() -1);
}

I remove the last character from the string before returning it because it is a "\n" new line character which is not good when I later append the name of my file contained in the package folder. I hope this could help.