2

I need to make a simple method in C++ to parse a string that's given as a path to a file (Example: "C:\root\filename.extension" )

What is the simplest way to do this? I've seen some examples with splitting a string, but I have several delimiters here, not just one type, (I have : \ and . ) so I'm not sure how it would work.

user1549053
  • 109
  • 3
  • 8
  • Also consider UIC-path and multiple extensions: \\some-pc\some-share\some-folder.dings\some-file.extension1.extension2. Perhaps you should split at \ and then split the parts by . and handling the first element seperately. – urzeit May 28 '13 at 10:43
  • 1
    It's full of pitfalls to parse Path strings correctly. If you want something you can just use try [boost filesystem](http://www.boost.org/doc/libs/1_53_0/libs/filesystem/doc/index.htm). If you just want to learn try on ... – πάντα ῥεῖ May 28 '13 at 10:48
  • 1
    is `boost::filesystem::path` an option for you? – billz May 28 '13 at 10:48
  • Also Qt library can help you http://qt-project.org/doc/qt-4.8/qdir.html#details – Martin Drozdik May 28 '13 at 10:50
  • I can use only the standard library (it's a school project, so arbitrary limitations like this apply). – user1549053 May 28 '13 at 11:00
  • This might be of use then: http://stackoverflow.com/questions/236129/splitting-a-string-in-c – akalenuk May 28 '13 at 11:27

2 Answers2

1

If the purpose is to actually deal with filesystem, use _splitpath, _makepath and related functions. It's not hard to create a wrapper C++ class that supports your use cases.

If it's just string manipulation, you shall define your rules first, as those can not be deduced. I.e. Win32 accepts both / and \ as dir separator, \ is redundant between actual directory elements but significant in prefix, etc.

Balog Pal
  • 16,195
  • 2
  • 23
  • 37
-1

boost library can help you, look : http://www.boost.org/doc/libs/1_53_0/libs/filesystem/doc/index.htm

PinkFloyd
  • 2,103
  • 4
  • 28
  • 47