# Where is _vscprintf

This topic is 5023 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So I'm compiling my code and msdn said to use _vscprintf to see how long a of a buffer I need to hold my formatted text. Msdn says Routine: _vscprintf Required header: <stdio.h> Compatibility: ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP Yet my code gives me errors!
*snipped*
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

*snipped*
va_list arg_list;

va_start(arg_list, text);

size = _vscprintf(text, arg_list) + 1;
buffer = new char [size];

vsprintf(buffer, text, arg_list);

va_end(arg_list);

The Output Window:
c:\navi0.6.75\source\navi_font.cpp(324) : error C2065: '_vscprintf' : undeclared identifier



##### Share on other sites
According to this Major Changes from Visual C++ 6.0 to Visual C++ .NET, _vscprintf is a new addition to Microsoft's C Run Time Library. Why they would describe it as if is available on older versions of Windows is beyond me. It's not exported by any of the crt dlls on my Windows 2000 machine.

##### Share on other sites
damn... so what would be an acceptable way around this? it also says vsprintf returns the number of characters written.

So something like...
va_list arg_list;char* buffer = new char [256];int size = 0;va_start(arg_list, text);size = vsprintf(buffer, text, arg_list);delete [] buffer;buffer = new char [size + 1];vsprintf(buffer, text, arg_list);			va_end(arg_list);

This WORKS... but i'm not comfortable with it. it'd cause a buffer overrun if the string was larger then 256, wouldnt it? even tho vsprintf will still write to it and return the correct size. i don't want to make any assumptions about the size of the string passed tho.

##### Share on other sites
I had a look at the vscprintf() code in the vs.net crt and it is not trivial enough to be posted on this forum :). It has deep roots in the crt (special FILE instance and so on). It spans over multiple files. It is a hell. Gosh.

I see two solutions:

1) redo a vscprintf() function. This is hard but it can be done. The format specification is known and writing a parser for the format string is not that hard. Maybe someone else wrote one and gave it to the world.

2) use this ugly code:

int count = 256;int written = 0;do {  char *buffer = new char[count];  written = vsnprintf(buffer, count, format, arg_list);  count *= 2;  delete [] buffer;  if (written < -1) {    return errorcode;  }} while (written == -1);buffer = new char[written+1];vsprintf(buffer, format, arg_list);

I know this is ugly. I did not test this code, and it may need some check (maybe you'll have to check errno). It takes advantage of vsnprintf() return code, which is (quoting the msdn):
Quote:
 _vsnprintf and _vsnwprintf return the number of characters written, not including the terminating null character, or a negative value if an output error occurs. If the number of characters to write exceeds count, then count characters are written and –1 is returned.

Since I did not test it, I'm not able to say wether the negative value (in case of error) is below -1 or not. Maybe -1 is an error and the function updates errno (to ETOOBIG for example). I had a look to the code and it seems that you cannot get any other negative return code but -1 - and in the case of vsnprintf() then -1 is set as the return type only if you hit the end of the string. But I may be wrong.

Anyway, this code is memory consuming and totally inneficient ;)

Regards,

##### Share on other sites
Quote:
 Original post by eklypsedamn... so what would be an acceptable way around this? it also says vsprintf returns the number of characters written.So something like...*** Source Snippet Removed ***This WORKS... but i'm not comfortable with it. it'd cause a buffer overrun if the string was larger then 256, wouldnt it? even tho vsprintf will still write to it and return the correct size. i don't want to make any assumptions about the size of the string passed tho.

If you're worried about buffer over flows, use vsnprintf (or related).

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 76
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632968
• Total Posts
3009585
• ### Who's Online (See full list)

There are no registered users currently online

×