在开发一个项目过程中,由于使用了最新版的开发环境,因此sprintf函数默认被开发环境禁止,出于养成好习惯的心思,我开始使用了sprintf_s函数,但这个函数着实坑了我一把,见下面代码:
DWORD mcoluncunt = 500;
LPBYTE mConfigInfoList = (LPBYTE)zalloc(mcoluncunt*200);
CString mTemp = "test for world";
sprintf_s((char*)mConfigInfoList,mcoluncunt * 200,"%s",mTemp);
看完MSDN一开始我就有点疑惑,为sprintf_s第二个参数要写成所分配的空间大小(这个错误的思路是从某篇文章上学到,哎,坑了一地),然后MSDN上给了详细例子:
#include <stdio.h>
int main( void )
{
char buffer[200], s[] = "computer", c = 'l';
int i = 35, j;
float fp = 1.7320534f;
// Format and print various data:
j = sprintf_s( buffer, 200, " String: %s\n", s );
j += sprintf_s( buffer + j, 200 - j, " Character: %c\n", c );
j += sprintf_s(