Sign in to follow this  

getting the relativ path

This topic is 3848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there an easy way to get the relative path from a base directory to a file? (Both are absolute paths) e.g. is there a function like "string relPath( string basedir, string targetfile );" ? using c/c++ and QT4 on Linux(so WinAPI won't help). thanks in advance. [Edited by - hydroo on June 1, 2007 7:32:39 AM]

Share this post


Link to post
Share on other sites
Also you could get the results you want by using std::strings. For instance:


#include <string>

std::string GetRelativeDir(const char * BaseDir, const char * FilePath)
{
std::string file = FilePath;
std::string basedir = BaseDir;
int length = basedir.length();

std::string relativedir = file.substr(length, file.length()-length);;

return relativedir;
}


So:


#include <iostream>

int main(int argc, char* argv[])
{
std::cout << GetRelativeDir("C:/Basedir/", "C:/Basedir/MuchStuff/Data/Moo/Boo/moo.txt").c_str() << std::endl;

return 0;
}


would output "MuchStuff/Data/Moo/Boo/moo.txt", which is the relative path to the file, relative to the given base directory.

Not sure if this is what you were looking for.. if not and I misunderstood, then I'm sorry. :)

Share this post


Link to post
Share on other sites

This topic is 3848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this