Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is sprintf_s analog of sprintf(newpath, "%s%s",...)?

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

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

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

like image 236
cnd Avatar asked Jan 18 '12 06:01

cnd


1 Answers

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

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).

like image 191
ugoren Avatar answered Sep 27 '22 23:09

ugoren