• ### Popular Now

• 9
• 16
• 15
• 12
• 9

#### Archived

This topic is now archived and is closed to further replies.

# Take out the filename of a path

This topic is 5655 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

If I have this path: "C:\Temp\test.txt", then I want to separate the path in two strings, "C:\Temp\" and "test.txt". I have the path i a WCHAR. So how do I code this?

##### Share on other sites
Take a look at _wsplitpath over at MSDN.

##### Share on other sites
You can do it with the STL, but I''m not very good with those things. You could std::reverse the sequence, std::find the first "\\" in the reversed sequence, and use std::string::substr with string::size - n to get the path.

If someone knows how to get rid of the ugliness and the inefficiency with standard algorithms or with boost, I''d be interested to know it.

Cédric

##### Share on other sites
cedricl: Since you want to find the first occurence of ''\'' from the rear, use std::string::find_last_of:
string str = "c:\\temp\\test.txt";int p0 = str.find_last_of( "\\" );string path, filename;path = str.substr( 0, p0-1 );file = str.substr( p0+1 );