Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to compare CString with TCHAR pointer.

CSstring sFile1 = _T(d:\\gh");
TCHAR szFile1 = new TCHAR[100];

szFile1 = "d:\\gh";

how to compare sFile1 with szFile1
Posted
Comments
barneyman 4-Dec-14 7:01am    
http://msdn.microsoft.com/en-us/library/aa315043(v=vs.60).aspx#_mfc_cstring_comparison

You may safely compare the two string using the CString == operator (see MSDN[^]), e.g.

C++
CString str = _T("FOO");
TCHAR s[] = _T("FOO");
if ( str == s )
{// they are equal
}


Please note, your code
Quote:
TCHAR szFile1 = new TCHAR[100];

szFile1 = "d:\\gh";

features, at least, two mistakes:
  • You are trying to assigning an array to a single TCHAR variable (compiler error).
  • If you fix the previous error then, with next statement you are discarding the reference to dynamically allocated memory (runtime bad behavior: memory leak).
 
Share this answer
 
v2
Try Something Like this:

C++
if(sFile1.Compare((CString)szFile1)==0)
{
//Equal
}
else{
//Not Equal
}


Here first typecast szFile1 to CString then compare it.
 
Share this answer
 
v2
Comments
CPallini 4-Dec-14 7:56am    
What are you doing?
Member 10641779 5-Dec-14 7:22am    
First I'm typecasting TCHAR to CString then just comparing them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900