如何实现以下数据类型的相互转换?
CString
string
char *
char xx[SIZE]
注意内存泄露

解决方案 »

  1.   

    http://dev.hoyod.net/article/vc/2004-06-14/4786.htm
      

  2.   

    准确地说是相互赋值或引用,好像没有string类型
    CString strVal;
    char* lpsz;
    char xx[SIZE];strVal = xx; /*将字符数组赋值给CString变量*/
    lpsz = xx; /*引用字符数组地指针*/lpsz = strVal.GetBuffer(strVal.GetLength()); /*取CString内部缓冲区指针,用strVal.ReleaseBuffer()释放*/
    strncpy(xx,(LPCTSTR)strVal,SIZE-1);/*拷贝CString地字符串到xx数组*/如果在UNICODE环境下还要用MultiByteToWideChar和WideCharToMultiBtyte转换
      

  3.   

    1.char * 到 CString:
        char sz[10];
        CString str;
        str.Format("%s", sz);
    2.CString 到 char *:
        Cstring str;
        int nLength = str.GetLength();
        char* sz = new char[nLength];
        sz = str.GetBuffer(0);
        ...
        str.ReleaseBuffer();
        delete [] sz;
      

  4.   

    CString --〉string
    str = string.c_str();CString <--string
    string.append((LPCTSTR)str)char * -> CString 
    str = char
      

  5.   

    我要的是转换后,二者没关系了。指针不指同一个地方。
    CString::GetBuffer(1)应该有问题吧,返回的指针可以被控制。这样吧,aa->a   bb->b
    char a[128];
    char b[128];
    CString aa="aa";
    string  bb="bb";转化后结果
    a[0]='a'   a[1]='a'  a[2]='\0'  a[3]='?'...
    b[0]='b'   b[1]='b'  b[2]='\0'  b[3]='?'...
      

  6.   

    不甚明白楼主的意思,可否这样:
        for(int i = 0; i < aa.GetLength() && i < 128; i++)
            a[i] = aa.GetAt(i);
        a[aa.GetLength()] = '\0';
      

  7.   

    int nLength = aa.GetLength();
    for(int i = 0; i < nLength && i < 128; i++)
        a[i] = aa.GetAt(i);
    if(nLength < 127) 
        a[nLength] = '\0';
      

  8.   

    对,就是酱紫的。string呢,呵呵
      

  9.   

    No have Type "string", You can named it by yourself.
      

  10.   

    #include <string>
    using namespace std;typedef basic_string<char, char_traits<char>, allocator<char> >
    string;就算吧,结贴