Sunday, 15 April 2012

windows - splitting file path in visual c++ -



windows - splitting file path in visual c++ -

i have filepath can have both / , \ , multiple of them, example

\\abc/tr\record.csv

or

\\re/nst/opr\etc/some/nov\

i first 2 pieces of (abc , tr in first illustration , re , nst in second)

how can in visual c++? (windows) regexes or msdn function? maybe there msdn function normalize filepath \ or / , 1 of them? (i cant utilize libs boost)

i think can "parse" string in loop if sure ever need encounter \ , / path separator. yes, can utilize regex or more complicated, it's pretty simple problem is.

something - note, code untested , won't compile, it'd illustrate algorithm:

std::string::citerator beg = path.cbegin(); std::string::citerator end = path.cend(); unsigned separator_count = 0; while(beg != end && separator_count < 4) { if (*beg == '/' || *beg == '\\') ++separator_count; ++beg; } std::string extracted_path = (separator_count == 3) ? path.substr(path.cbegin(), std::distance(path.cbegin(), beg)) : std::string();

windows visual-c++ split

No comments:

Post a Comment