How to determine buffer size for vswprintf on Linux gcc

I need to allocate a sufficient buffer for the vswprintf () format function. When you do the same with the ANSI string, I use:

vsnprintf( NULL, NULL, pszFormat, args );

which returns me the required buffer size. But it looks like the Unicode version of this function does not have this function. When I execute:

vswprintf( NULL, NULL, pszFormat, args );

The result value is always -1.

The only solution I found was to use a large static buffer to calculate the required size. But I do not like this solution:

static const int nBuffSize = 1024;
static XCHAR evalBuff[nBuffSize];
int nSize = vswprintf( evalBuff, nBuffSize, pszFormat, args );
if ( nSize != -1 )
{
 return nSize;
}
else
{
 throw XXX;
}

Is there a way to measure the size of the required buffer for unicode strings?

Relations Ludek

+5
source share
3 answers

/dev/null ( NUL ) ( , ):

#if! defined (_MSC_VER)
  printf_dummy_file = fopen ( "/dev/null", "wb" );
#else
  printf_dummy_file = fopen ( "NUL", "wb" );
#endif

...

n = vfprintf (printf_dummy_file, format, args);

"fopen" ( , ++).

+4

, , , :

std::vector<wchar_t> vec(512);
int nSize;
do {
    vec.resize(vec.size()*2);
    va_list args2;
    va_copy args2, args;
    nSize = vswprintf(&vec[0], vec.size(), format, args2);
    va_end args2
} while(nSize < 0);
// now I have the length, and the formatted string to copy if required.

POSIX, , errno errno while, , . errno, http://opengroup.org/onlinepubs/007908775/xsh/fwprintf.html, , , , ( ).

- wostringstream. , .

+1

This will get the buffer size:

vswprintf (nullptr, -1, pszFormat, args);

-1
source

All Articles