Sign in to follow this  

Where is _vscprintf

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by eklypse
damn... 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).

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this