Path from filename

Started by
7 comments, last by Aprosenf 18 years, 10 months ago
Is there any function already existing in the windows api or directx that can return a path from a full filename? Thanks
Advertisement
I know there's a newer function for this, but all I can think of is _splitpath.
Raeldor, can you explain me, what difference between "full filename" and "file path"?:)
Quote:Original post by Daevaorn
Raeldor, can you explain me, what difference between "full filename" and "file path"?:)


Full filename would be something like...

c:\textures\test.bmp

file path would be...

c:\textures

Rael
Oh, ok. If you are programming in C++, there is path class in boost::filesystem
lib.

#include <Boost/filesystem/path.hpp>namespace fs = boost::filesystem;//...fs::path p( "c:\textures\test.bmp" );std::string path = p.branch_path().string(); //c:\textures//...


Quote:Original post by Daevaorn
Oh, ok. If you are programming in C++, there is path class in boost::filesystem
lib.

#include <Boost/filesystem/path.hpp>namespace fs = boost::filesystem;//...fs::path p( "c:\textures\test.bmp" );std::string path = p.branch_path().string(); //c:\textures//...


Hi, thanks for the reply, sorry for the delay. What is this boost library? Is there nothing in the standard windows library for this?
Thanks
Quote:Original post by Raeldor
Hi, thanks for the reply, sorry for the delay. What is this boost library? Is there nothing in the standard windows library for this?
Thanks


The boost library is like the STL, but not "standard". You'll find it at www.boost.org

And what's wrong with _splitpath()? Sure, it's not fancy-shmancy, and requires you to jam the drive & path outputs back together, but it works.
Quote:Original post by pragma Fury
Quote:Original post by Raeldor
Hi, thanks for the reply, sorry for the delay. What is this boost library? Is there nothing in the standard windows library for this?
Thanks


The boost library is like the STL, but not "standard". You'll find it at www.boost.org

And what's wrong with _splitpath()? Sure, it's not fancy-shmancy, and requires you to jam the drive & path outputs back together, but it works.


Ah, my bad... when you were referring to _splitpath I thought you meant you couldn't remember the function name (didn't know functions could start with '_'). I just found the documentation for _splitpath.

Thank you! :)
Or you could just grab everything before the last backslash, i.e.

In C++:
std::string filename;std::string path = filename.substring(0, filename.find_last_of('\\'));


In C:
char *filename;char path[256];char *lastBackslash = strchr(filename, '\\');strncpy(path, filename, lastBackslash - filename + 1);path[lastBackslash - filename + 1] = '\0';

This topic is closed to new replies.

Advertisement