CString相关

    技术2022-05-12  14

    A   在debug时无法查看CString类型的值?

    在生存期内,需要全编译一次工程,这是因为CString类型的特殊设计,这样才能观察CString类型变量的值。

     

    B  CString类型有大小限制嘛?

    调试是只显示部分数据,可能是为了不过分占据桌面窗口,而其在内存中数据是正确的。

    A CString object can store up to INT_MAX (2,147,483,647) characters. The TCHAR data type is used to get or set individual characters inside a CString object. Unlike character arrays, the CString class has a built-in memory allocation capability. This allows CString objects to automatically grow as needed (that is, you don’t have to worry about growing a CString object to fit longer strings).

     

    C   CString类型和string,char *的转换

    引自:http://www.cppblog.com/robinson119/archive/2007/04/26/22870.aspx,有更改

     

    这三种类型各有各的优点,比如

    CString比较灵活,是基于MFC常用的类型,安全性也最高,但可移植性最差。

    string是使用STL时必不可少的类型,所以是做工程时必须熟练掌握的;

    char*是从学习C语言开始就已经和我们形影不离的了,有许多API都是以char*作为参数输入的。所以熟练掌握三者之间的转换十分必要。

    转换方法如下:

    1 string to CString   

      CString.format("%s",string.c_str()); 

    2 CString to string

    string str(CString.GetBuffer(str.GetLength()));

    3 string to char *

    char *p=string.c_str();

    4 char * to string

    string str(char*);

    5 CString to char *

    a. 强制转换

            CString theString( (_T("Char test "));        LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;

    b. strcpy    strcpy(char,CString,sizeof(char));

           CString theString( (_T("Char test "));       LPTSTR lpsz = new TCHAR[theString.GetLength()+1];        _tcscpy(lpsz, theString);  需要说明的是,strcpy(或可移值的_tcscpy)的第二个参数是 const wchar_t* (Unicode)或const char* (ANSI),系统编译器将会自动对其进行转换。

    c. CString::GetBuffer

     

       如果你需要修改 CString 中的内容,它有一个特殊的方法可以使用,那就是 GetBuffer,它的作用是返回一个可写的缓冲指针。 如果你只是打算修改字符或者截短字符串,例如:       CString s(_T("Char test "));        LPTSTR p = s.GetBuffer(); 

            LPTSTR dot = strchr(p, ''.'');

             // 在这里添加使用p的代码

              if(p != NULL) 

             *p = _T('');         s.ReleaseBuffer();                     // 使用完后及时释放,以便能使用其它的CString成员函数

             在 GetBuffer 和 ReleaseBuffer 之间这个范围,一定不能使用你要操作的这个缓冲的 CString 对象的任何方法。因为 ReleaseBuffer 被调用之前,该 CString 对象的完整性得不到保障。

    6 char * to CString

    a. 直接赋值

    b. CString.format("%s",char*);

    e.g:

            char chArray[] = "Char  test";         TCHAR * p = _T("Char  test");( 或LPTSTR p = _T("Char  test");)         CString theString = chArray;         theString.Format(_T("%s"), chArray);         theString = p;

     CString的format方法是非常好用的。string的c_str()也是非常常用的,但要注意和char *转换时,要把char定义成为const char*,这样是最安全的。

     

    D    其它数据类型转换为字符串         1。短整型(int)     itoa(i,temp,10);///将i转换为字符串放入temp中,最后一个数字表示十进制     itoa(i,temp,2);   ///按二进制方式转换       2。长整型(long)     ltoa(l,temp,10);       3。浮点数(float,double)     用fcvt可以完成转换,这是MSDN中的例子:     int   decimal,   sign;       char   *buffer;       double   source   =   3.1415926535;       buffer   =   _fcvt(   source,   7,   &decimal,   &sign   );       运行结果:source:   3.1415926535   buffer:   '31415927'   decimal:   1   sign:   0     decimal表示小数点的位置,sign表示符号:0为正数,1为负数       4。CString变量     str   =   "2008北京奥运";     buf   =   (LPSTR)(LPCTSTR)str;       5。BSTR变量     BSTR   bstrValue   =   ::SysAllocString(L"程序员");       char   *   buf   =   _com_util::ConvertBSTRToString(bstrValue);       SysFreeString(bstrValue);       AfxMessageBox(buf);       delete(buf);       6。CComBSTR变量     CComBSTR   bstrVar("test");       char   *buf   =   _com_util::ConvertBSTRToString(bstrVar.m_str);       AfxMessageBox(buf);       delete(buf);           7。_bstr_t变量     _bstr_t类型是对BSTR的封装,因为已经重载了=操作符,所以很容易使用     _bstr_t   bstrVar("test");       const   char   *buf   =   bstrVar;///不要修改buf中的内容       AfxMessageBox(buf);               8。通用方法(针对非COM数据类型)     用sprintf完成转换     char     buffer[200];     char     c   =   '1';     int       i   =   35;     long     j   =   1000;     float   f   =   1.7320534f;     sprintf(   buffer,   "%c",c);     sprintf(   buffer,   "%d",i);     sprintf(   buffer,   "%d",j);     sprintf(   buffer,   "%f",f);


    最新回复(0)