PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 12, 2022

[FIXED] How to split a path into separate strings?

 September 12, 2022     boost, c++, cross-platform, path, split     No comments   

Issue

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.


Solution

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



Answered By - sehe
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing