idanshmu
idanshmu

Reputation: 5261

How to split a path into separate strings?

This is a complimentary question to:
How to build a full path string (safely) from separate strings?

So my question, how to split a path into separate strings in a cross platform manner.

This solution, using Boost.Filesystem is very elegant and Boost must have implemented some splitPath() function. I couldn't find any.

Note: bear in mind that I can do this task myself but I'm more interested in a closed box solution.

Upvotes: 10

Views: 11652

Answers (4)

Mayank Joshi
Mayank Joshi

Reputation: 80

If you want to do everything manually without, using any library, then this will help. It splits the given full path into corresponding names and store them in a vector.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    string filePath = "C:\\ProgramData\\Users\\CodeUncode\\Documents";
    vector<string> directories;
    size_t position=0, currentPosition=0;
    
    while(currentPosition != -1)
    {
        currentPosition = filePath.find_first_of('\\', position);
        directories.push_back(filePath.substr(position,currentPosition-position));
        position = currentPosition+1;
    }
    for(vector<string>::iterator it = directories.begin(); it!=directories.end(); it++)
        cout<<*it<<endl;

    return 0;
} 

Output:

C:
ProgramData
Users
CodeUncode
Documents

Upvotes: 1

Jason Harrison
Jason Harrison

Reputation: 965

If you don't have C++11 auto, or are writing cross-platform code where boost::filesystem::path might be std::wstring:

std::vector<boost::filesystem::path> elements;
for (boost::filesystem::path::iterator it(filename.begin()), it_end(filename.end()); it != it_end; ++it) 
{
    elements.push_back(it->filename());
}

Upvotes: 1

sehe
sehe

Reputation: 393009

Indeed, there is path_iterator. But if you want elegance:

#include <boost/filesystem.hpp>

int main() {
    for(auto& part : boost::filesystem::path("/tmp/foo.txt"))
        std::cout << part << "\n";
}

Prints:

"/"
"tmp"
"foo.txt"

And

    for(auto& part : boost::filesystem::path("/tmp/foo.txt"))
        std::cout << part.c_str() << "\n";

prints

/
tmp
foo.txt

No need to worry about the moving parts

Upvotes: 15

ALittleDiff
ALittleDiff

Reputation: 1211

std::vector<std::string> SplitPath(const boost::filesystem::path &src) {
    std::vector<std::string> elements;
    for (const auto &p : src) {
        elements.emplace_back(p.filename());
    }
    return elements;
}

Upvotes: 5

Related Questions