sprintf_s模拟的sprintf(newpath,“%s%s”,…)?

时间:2021-08-30 20:21:20

I'm using sprintf(newpath, "%s%s", cCurrentPath, "\\init.scm"); to add \init.scm to current dir path but there is the usual warning:

我使用sprintf(newpath, "%s%s", cCurrentPath, "\\init.scm");添加\ init。单片机到当前的dir路径,但有通常的警告:

warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

警告C4996:“sprintf”:这个函数或变量可能不安全。考虑使用sprintf_s代替。若要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS。

Sprintf_s doesn't support such "%s%s" string sum. How can I do it using sprintf_s?

Sprintf_s不支持这样的“%s%s”字符串和。我怎么用sprintf_s来做呢?

1 个解决方案

#1


14  

sprintf_s is basically the same as sprintf, but it gets another parameter:

sprintf_s与sprintf基本相同,但它得到了另一个参数:

sprintf_s(newpath, sizeof(newpath), "%s%s", cCurrentPath, "\\init.scm");

Note - if newpath is a normal character array, sizeof(newpath) works. If it's a pointer or an array passed as an argument, you may need a different way to get the size.
You can also use snprintf for the same purpose in a non-MS environment (though it works differently).

注意:如果newpath是一个正常字符数组,sizeof(newpath)工作。如果是作为参数传递的指针或数组,则可能需要另一种方法来获取大小。您也可以在非ms环境中使用snprintf(尽管它的工作方式不同)。

#1


14  

sprintf_s is basically the same as sprintf, but it gets another parameter:

sprintf_s与sprintf基本相同,但它得到了另一个参数:

sprintf_s(newpath, sizeof(newpath), "%s%s", cCurrentPath, "\\init.scm");

Note - if newpath is a normal character array, sizeof(newpath) works. If it's a pointer or an array passed as an argument, you may need a different way to get the size.
You can also use snprintf for the same purpose in a non-MS environment (though it works differently).

注意:如果newpath是一个正常字符数组,sizeof(newpath)工作。如果是作为参数传递的指针或数组,则可能需要另一种方法来获取大小。您也可以在非ms环境中使用snprintf(尽管它的工作方式不同)。