请教CString的Format问题

enmoswuthering 2017-09-03 12:08:54
各位大神们:
我在网上查到的
CString str = "Some Data";
str.Format("%s%d", str, 123); //Attention: str is also used in the parameter list.
这样写可能会导致buffer too small的问题

现在有以下问题:
(1)以下句子会导致buffer too small的问题吗:
DWORD ret = GetPrivateProfileString( "PARA", "train", "", testString.GetBuffer(MAX_PATH), MAX_PATH ,"E:\\test.ini");
testString.Format("%s",testString.GetBuffer());

(2)
“f:\dd\vctools\crt_bld\self_x86\crt\src\vsprintf.c Line:244”
Expression:("Buffer too small", 0).
这个问题是针对format的报错吗?还是其他问题也会导致这个错误?我在网上查到的其他串处理如果出错也会报buffer too small,但是位置不在Line:244
...全文
252 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
worldy 2017-09-06
  • 打赏
  • 举报
回复
 CString str1 = "Some Data",str;
  str.Format("%s%d", str1, 123);
纠正一下上楼的错误
worldy 2017-09-06
  • 打赏
  • 举报
回复
CString str1 = "Some Data",str;
  str.Format("%s%d", str, 123);
这样使用才是安全的
zgl7903 2017-09-04
  • 打赏
  • 举报
回复
建议用一个中间变量缓冲一下, 如果函数的参数中没有考虑输入输出相同时,很容易出问题 CString strTmp(str); str.Format(_T("%s"), (LPCTSTR)strTmp);
赵4老师 2017-09-03
  • 打赏
  • 举报
回复
不要迷信书、考题、老师、回帖; 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实! 代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。 崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止
enmoswuthering 2017-09-03
  • 打赏
  • 举报
回复
急,在线等
homesos 2017-09-03
  • 打赏
  • 举报
回复
引用 8 楼 enmoswuthering 的回复:
[quote=引用 7 楼 schlafenhamster 的回复:] 所以 不要 这样用 testString.Format("%s",testString.GetBuffer()); CString tmp=testString; testString.Empty(); testString.Format("%s", tmp);
testString.Format("%s",testString.GetBuffer()); 这个句子在一个产品里用了8年了都没有出问题,现在出了一个buffer too small的问题,所以一直怀疑又不敢确定啊 [/quote] CString,不能自己做运算再赋给自己,我以前也遇到过。
enmoswuthering 2017-09-03
  • 打赏
  • 举报
回复
引用 7 楼 schlafenhamster 的回复:
所以 不要 这样用 testString.Format("%s",testString.GetBuffer()); CString tmp=testString; testString.Empty(); testString.Format("%s", tmp);
testString.Format("%s",testString.GetBuffer()); 这个句子在一个产品里用了8年了都没有出问题,现在出了一个buffer too small的问题,所以一直怀疑又不敢确定啊
schlafenhamster 2017-09-03
  • 打赏
  • 举报
回复
所以 不要 这样用 testString.Format("%s",testString.GetBuffer()); CString tmp=testString; testString.Empty(); testString.Format("%s", tmp);
schlafenhamster 2017-09-03
  • 打赏
  • 举报
回复
CString::Format void Format( LPCTSTR lpszFormat, ... ); void Format( UINT nFormatID, ... ); Parameters lpszFormat A format-control string. nFormatID The string resource identifier that contains the format-control string. Remarks Call this member function to write formatted data to a CString in the same way that sprintf formats data into a C-style character array. This function formats and stores a series of characters and values in the CString. Each optional argument (if any) is converted and output according to the corresponding format specification in lpszFormat or from the string resource identified by nFormatID. The call will fail if the string object itself is offered as a parameter to Format. For example, the following code: CString str = "Some Data"; str.Format("%s%d", str, 123); // Attention: str is also used in the parameter list. will cause unpredictable results. When you pass a character string as an optional argument, you must cast it explicitly as LPCTSTR. The format has the same form and function as the format argument for the printf function. (For a description of the format and arguments, seeprintf in the Run-Time Library Reference.) A null character is appended to the end of the characters written. For more information, seesprintf in the Run-Time Library Reference. str.Format("%s%d", str, 123); // Attention: str is also used in the parameter list. // 注意str 同时被用作参数
enmoswuthering 2017-09-03
  • 打赏
  • 举报
回复
引用 3 楼 VisualEleven 的回复:
CString str = TEXT("Some Data"); str.AppendFormat(TEXT("%d"), 123); DWORD ret = GetPrivateProfileString( TEXT("PARA"), TEXT("train"), TEXT(""), testString.GetBuffer(MAX_PATH), MAX_PATH ,TEXT("E:\\test.ini")); testString.RelaseBuffer();
CString str; CString teststring= _T("1234"); str.Format("%s",teststring); Format用%s有没有安全隐患?
enmoswuthering 2017-09-03
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
不要迷信书、考题、老师、回帖; 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实! 代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。 崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止
因为这个问题才出现过一次,弹出了buffer too small的问题,现在重现不了这个问题,没法用CallStack调试,想请教一下是否能确认那两句语句代码有问题呢?
Eleven 2017-09-03
  • 打赏
  • 举报
回复
CString str = TEXT("Some Data"); str.AppendFormat(TEXT("%d"), 123); DWORD ret = GetPrivateProfileString( TEXT("PARA"), TEXT("train"), TEXT(""), testString.GetBuffer(MAX_PATH), MAX_PATH ,TEXT("E:\\test.ini")); testString.RelaseBuffer();

16,471

社区成员

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

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

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