[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 507
  • Last Modified:

A CString version of strtok() function

// m_directory hold a directory path with a *. extension in the string, want to cutoff the extension.
CString fileDir;
char* pTemp = strdup((LPCTSTR)m_directory);
fileDir = CString(strtok(pTemp,"*"));
free(pTemp);

Is there an easier way to do this directly to the CString?
0
galneweinhaw
Asked:
galneweinhaw
  • 3
1 Solution
 
Jaime OlivaresCommented:
Assuming m_directory is also CString type, just you need:

CString fileDir = m_directory.SpanExcluding("*");
0
 
Jaime OlivaresCommented:
In case m_directory is any kind of old-C string (char *), then you need something like this:

CString fileDir = CString(m_directory).SpanExcluding("*");

Good luck,
Jaime.
0
 
jkrCommented:
Check out http://www.codeproject.com/string/tokenizer.asp ("CTokenizer - A simple tokenizer class that can be used on CStrings"). You'd use it like

CTokenizer tok(_T("A-B+C*D-E"), _T("-+"));
CString cs;

But, you simply could use

int nPos = m_directory.ReverseFind('*');

if ( -1 != nPos) { // cut off the rest

m_directory = m_directory.Left(nPos - 1);
}
0
 
Jaime OlivaresCommented:
I think this kind of problems doesn't need any kind of tokenizer function, since you just want to detect a single ocurrence of a character. Due nature of wildcard path, you don't need a reverse find also, because an asterisk could only occur once.
So, SpanExcluding will copy string contents from one CString to another UNTIL an specific character or group of characters is found. Have a look to:
http://msdn.microsoft.com/library/en-us/vcmfc98/html/_mfc_cstring.3a3a.spanexcluding.asp
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now