Extract the file name from the full file path.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 185
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: Extract the file name from the full file path.

 
0
  #11
Nov 9th, 2007
Originally Posted by Salem View Post
> if(ss.ReverseFind('\\')== 8) What does this return if there is no \\ ?
That what I'm really worried. That number is not fix always. It means, \\ can be missing in such instances.

I've try something like this also,
  1. CString aa = fileName.Right(fileName.GetLength() - (fileName.ReverseFind('\\') + 1));

Actually it gives the file path. Then use ReverseFind() again and find the '.', use a Left() to get the name only.

As you say, if the extension is missing this wont work.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Extract the file name from the full file path.

 
0
  #12
Nov 9th, 2007
Like I said, read the manual and find out what is returned when there is nothing to find, and use the appropriate if / else constructs to deal with it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1
Reputation: hINTModuleState is an unknown quantity at this point 
Solved Threads: 0
hINTModuleState hINTModuleState is offline Offline
Newbie Poster

Using std::string, the solution would be :

 
-1
  #13
Oct 26th, 2009
std::wstring PathToFileName(std::wstring strPath)
{
size_t found1 = strPath.rfind(L'\\');
size_t found2 = strPath.rfind(L'/');

size_t found = (found1!=std::wstring::npos) ? ( (found2!=std::wstring::npos) ? max(found1,found2) : found1 ) : found2;

std::wstring result = strPath.substr(found+1, strPath.length() - found-5);

return result;
}
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 14749 | Replies: 12
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC