getting the relativ path
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]
If you can use the classes within the boost::filesystem namespace they will provide you with the functionality you are looking for.
Also you could get the results you want by using std::strings. For instance:
So:
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. :)
#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. :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement