关于GetPrivateProfileString和CString的问题,请指点

potatoli 2004-01-29 02:26:11
有如下的代码:
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]就正常了。
...全文
196 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
potatoli 2004-01-29
  • 打赏
  • 举报
回复
问题解决了,goodseener(西湖醋鱼)和bendou16(跳动的心) 的方法都可以,谢谢大家
bendou16 2004-01-29
  • 打赏
  • 举报
回复
::GetPrivateProfileString( section, key, _T(""), str1.GetBuffer(20), 20, inifile);
最好完成上句后
调用str1.GetBufferSetLength( nlength );
bendou16 2004-01-29
  • 打赏
  • 举报
回复
我分析的原因是::由于str1的数据存储buffer被强制改成了20,所以在执行 “+=”的时候, 申请分配新存储空间时出错。
bendou16 2004-01-29
  • 打赏
  • 举报
回复
str2 = str1; 改为 str2 = str1.GetBuffer( nlength );
//nlength 为str1中实际数据的长度,如果是"abc",那么nlength=3;
str2 += "abc";

祝你好运
goodseener 2004-01-29
  • 打赏
  • 举报
回复
在::GetPrivateProfileString( section, key, _T(""), str1.GetBuffer(20), 20, inifile);
后加上str1.ReleaseBuffer();
potatoli 2004-01-29
  • 打赏
  • 举报
回复
to taianmonkey()
还是不行啊
taianmonkey 2004-01-29
  • 打赏
  • 举报
回复
str2 += _T("abc");
改成:
str2 = str2 + _T("abc");
potatoli 2004-01-29
  • 打赏
  • 举报
回复
to windyloft(神在看着你)
可是 "+="是CString重载了的呀?
windyloft 2004-01-29
  • 打赏
  • 举报
回复
和GetPrivateProfileString没关系,
只是str2 += _T("abc");这一步出现了差异
windyloft 2004-01-29
  • 打赏
  • 举报
回复
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
);

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧