将CString与TCHAR指针进行比较 [英] Compare CString with TCHAR pointer
本文介绍了将CString与TCHAR指针进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何比较CString和TCHAR指针。
CSstring sFile1 = _T(d:\\\\);
TCHAR szFile1 = new TCHAR [100];
szFile1 =d:\\gh;
如何比较sFile1和szFile1
解决方案
您可以使用CString ==
运算符安全地比较两个字符串(参见MSDN [ ^ ]),例如
CString str = _T( FOO);
TCHAR s [] = _T( FOO);
if (str == s)
{ // 它们相等
}
请注意,您的代码
引用:TCHAR szFile1 = new TCHAR [100];
szFile1 =d: \\\\;
至少有两个错误:
- 您正在尝试将数组分配给a单个
TCHAR
变量(编译器错误)。- 如果修复了上一个错误,则使用next语句丢弃对动态分配内存的引用(运行时)不良行为:内存泄漏)。
试试这样的东西:
if (sFile1.Compare((CString)szFile1)== 0 )
{
// 等于
}
其他 {
// 不等于
}
这里先将类型转换为szFile1到CString然后比较它。
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解决方案You may safely compare the two string using theCString ==
operator (see MSDN[^]), e.g.
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).
Try Something Like this:
if(sFile1.Compare((CString)szFile1)==0) { //Equal } else{ //Not Equal }
Here first typecast szFile1 to CString then compare it.
这篇关于将CString与TCHAR指针进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文