各位大神们:
我在网上查到的
 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

解决方案 »

  1.   

    不要迷信书、考题、老师、回帖;
    要迷信CPU、编译器、调试器、运行结果。
    并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。
    任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实!代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
    提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
    单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止
      

  2.   

    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();
      

  3.   

    因为这个问题才出现过一次,弹出了buffer  too small的问题,现在重现不了这个问题,没法用CallStack调试,想请教一下是否能确认那两句语句代码有问题呢?
      

  4.   

    CString str;
    CString teststring= _T("1234");
    str.Format("%s",teststring);Format用%s有没有安全隐患?
      

  5.   

    CString::Format
    void Format( LPCTSTR lpszFormat, ... );void Format( UINT nFormatID, ... );ParameterslpszFormatA format-control string.nFormatIDThe string resource identifier that contains the format-control string. ResCall 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 同时被用作参数
      

  6.   

    所以 不要 这样用
    testString.Format("%s",testString.GetBuffer());
    CString tmp=testString; testString.Empty();
    testString.Format("%s", tmp);
      

  7.   

    testString.Format("%s",testString.GetBuffer()); 这个句子在一个产品里用了8年了都没有出问题,现在出了一个buffer too small的问题,所以一直怀疑又不敢确定啊
      

  8.   

    testString.Format("%s",testString.GetBuffer()); 这个句子在一个产品里用了8年了都没有出问题,现在出了一个buffer too small的问题,所以一直怀疑又不敢确定啊
    CString,不能自己做运算再赋给自己,我以前也遇到过。
      

  9.   

    建议用一个中间变量缓冲一下, 如果函数的参数中没有考虑输入输出相同时,很容易出问题CString strTmp(str);
    str.Format(_T("%s"),  (LPCTSTR)strTmp);
      

  10.   

     CString str1 = "Some Data",str;
      str.Format("%s%d", str, 123); 这样使用才是安全的
      

  11.   

     CString str1 = "Some Data",str;
      str.Format("%s%d", str1, 123); 纠正一下上楼的错误