Extracting file name from path

Hello everyone,
I have a string like this
 
const char *filename = "C:/Qt/progetti/worlds/fasr.world";

then I have a string like this
 
char *pathdir = "C:/Qt/progetti/worlds";

I would get this string: "worlds/fasr.world" how should I do ?
Or, if you're using Qt:

QDir(pathdir).relativeFilePath(filename);

Although in this case your pathdir should be "C:/Qt/progetti".
Thanks for the responses, but my problem is obtaining only the final part of the filename string, ie "worlds/fasr.world" ("worlds/" which is a part of path and "fasr.world" which is the name of the file)
std::string( basename(pathdir) ) + std::string("/") + std::string( basename(filename) );
Thanks ne555
Or you could use boost for a more portable solution. (The code is nearly the same.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <boost/filesystem.hpp>

namespace fs = boost::filesystem ;

int main()
{
    const char *filename = "C:/Qt/progetti/worlds/fasr.world";
    char *pathdir = "C:/Qt/progetti/worlds";

    std::cout << fs::basename(pathdir) + '/' + 
        fs::basename(filename) + fs::extension(filename) << '\n' ;

    // Or:
    //fs::path path_with_name("C:/Qt/progetti/worlds/fasr.world") ;
    //fs::path base_path("C:/Qt/progetti/worlds") ;

    //std::cout << base_path.filename().string() + '/' + 
        // path_with_name.filename().string() << '\n' ;

}
worlds/fasr.world
Topic archived. No new replies allowed.