有如下的代码:
CString str1 = _T("");
::GetPrivateProfileString( section, key, _T(""), str1.GetBuffer(20), 20, inifile);
其中section, key和inifile都是和程序相关的。这一步执行完成之后,str1的内容是"123"。然后
CString str2 = str1;
这一步str2的内容也是"123"。然后
str2 += _T("abc");
这一步str2的内容还是"123"。而不是"123abc"。请问这是为什么?
另外,如果把str1换成char[20]就正常了。

解决方案 »

  1.   

    The GetPrivateProfileString function retrieves a string from the specified section in an initialization file. This function is provided for compatibility with 16-bit Windows-based applications. Win32-based applications should store initialization information in the registry. DWORD GetPrivateProfileString(
      LPCTSTR lpAppName,        // points to section name
      LPCTSTR lpKeyName,        // points to key name
      LPCTSTR lpDefault,        // points to default string
      LPTSTR lpReturnedString,  // points to destination buffer
      DWORD nSize,              // size of destination buffer
      LPCTSTR lpFileName        // points to initialization filename
    );
      

  2.   

    和GetPrivateProfileString没关系,
    只是str2 += _T("abc");这一步出现了差异
      

  3.   

    to windyloft(神在看着你) 
    可是 "+="是CString重载了的呀?
      

  4.   

    str2 += _T("abc");
    改成:
    str2 = str2 + _T("abc");
      

  5.   

    to taianmonkey() 
    还是不行啊
      

  6.   

    在::GetPrivateProfileString( section, key, _T(""), str1.GetBuffer(20), 20, inifile);
    后加上str1.ReleaseBuffer();
      

  7.   

    str2 = str1;   改为  str2 = str1.GetBuffer( nlength );
    //nlength 为str1中实际数据的长度,如果是"abc",那么nlength=3;
    str2 += "abc";祝你好运
      

  8.   

    我分析的原因是::由于str1的数据存储buffer被强制改成了20,所以在执行 “+=”的时候,  申请分配新存储空间时出错。
      

  9.   

    ::GetPrivateProfileString( section, key, _T(""), str1.GetBuffer(20), 20, inifile);
    最好完成上句后
    调用str1.GetBufferSetLength( nlength );
      

  10.   

    问题解决了,goodseener(西湖醋鱼)和bendou16(跳动的心) 的方法都可以,谢谢大家