2

I currently use _snprintf(NULL,0,fmtStr, Str..) and it returns the required size of the buffer. I allocate them and then invoke _snprintf again to fill the buffer. But currently _snprintf is deprecated and _snprintf_s is the recommended function. But according to msdn it doesn't seem to return length without allocating buffer before-hand.

Is there a way around this?

Ilya
  • 4,583
  • 4
  • 26
  • 51
Vignesh Murugesan
  • 747
  • 1
  • 5
  • 20
  • Use `snprintf` ? The C++ standard requires this function to exist, and it is not deprecated. – M.M Sep 16 '15 at 02:37
  • I don't think `_snprintf` is likely to be *removed*; the deprecation warning is for developers following the Security Development Lifecycle (where it's a banned function). – user253751 Sep 16 '15 at 02:51
  • @M.M If MSVC does have `snprintf`, then it will also be marked as deprecated, as it's also banned in the SDL. (You can define a macro to disables these warnings, but I don't remember what it's called) – user253751 Sep 16 '15 at 03:07
  • I'm struggling to see how `_snprintf_s` is supposed to be more secure than `snprintf`. – M.M Sep 16 '15 at 03:59
  • 1
    Vignesh: assuming you have *some* idea of the likely size of the string, you could write the string into a stack-based buffer (of fixed size) and then copy it to your newly allocated buffer. (Or, of course, use C++ strings.) – Harry Johnston Sep 16 '15 at 04:17
  • @HarryJohnston ISO C++ `snprintf` does guarantee null-termination – M.M Sep 16 '15 at 04:17
  • @M.M: the Microsoft version doesn't: https://msdn.microsoft.com/en-us/library/2ts7cx93.aspx - guess they haven't caught up with that part of the standard either. (Though I'm not sure how they're supposed to, since it would break backwards compatibility.) – Harry Johnston Sep 16 '15 at 04:18
  • @HarryJohnston that is `_snprintf`, not `snprintf` . I could agree with `_snprintf` being deprecated for `_snprintf_s`. – M.M Sep 16 '15 at 04:20
  • @M.M: ah, quite right. [As it turns out,](https://msdn.microsoft.com/en-us/library/bb288454.aspx) `_snprintf` is banned, but `snprintf` isn't - it's on the list of *recommended* functions! (Which probably also means that they have implemented `snprintf` in the latest version after all, it just isn't in the version I'm using.) – Harry Johnston Sep 16 '15 at 04:28
  • Vignesh, what version of Visual Studio are you using? – Harry Johnston Sep 16 '15 at 04:29
  • Hi @Harry. I'm using VS2015 community-edition. Seems snprintf is a C99 standard and VS may not support it yet. – Vignesh Murugesan Sep 16 '15 at 05:28
  • Think I'll allocate a buffer[MAX_PATH] and take it from there. This is inline with Harry's suggestion. Thanks for that! – Vignesh Murugesan Sep 16 '15 at 05:33
  • It's the C++ standard that's relevant, because MS are trying to catch up to that one. (Whereas if I understand correctly they've said they don't care about C99 and don't intend to ever support it.) Are you `#include`ing `cstdio` or `stdio.h` ? Is it a .c file or a .cpp file? – Harry Johnston Sep 16 '15 at 21:56
  • It's a CPP file. I had included stdio.h. I tried cstdio also- did not help. – Vignesh Murugesan Sep 16 '15 at 22:53
  • The function should exist, according to http://stackoverflow.com/a/27754829/886887 – Harry Johnston Sep 16 '15 at 23:34

1 Answers1

2

In Windows : Before VS2015: Make use of _vscprintf() to get the size of the expanded format string. Allocate memory and use vsnprintf_s() to perform the actual buffer fill.

Gcc/Windows-VS2015 onward: (thanks to Harry-Johnston for pointing this out) snprintf() should be available. Use snprintf(NULL,0,...) to get the size, allocate mem, then snprintf(buffer,..) to perform the actual buffer fill.

Vignesh Murugesan
  • 747
  • 1
  • 5
  • 20
  • For **wchar_t** snprintf does not work. I have tried to remplace 'snprint' with **_snwprintf **', but I get a C4996 error. – Tom Tom May 15 '20 at 14:29