Jump to content
  • Advertisement
Sign in to follow this  
hydroo

getting the relativ path

This topic is 4132 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
Advertisement
If you can use the classes within the boost::filesystem namespace they will provide you with the functionality you are looking for.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!