Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to find length of va_list string?

Status
Not open for further replies.

jorgander

Programmer
Jul 15, 2003
11
0
0
US
i've looked around for quite a while now, but can't find any reasonable solution for this. i'm trying to write a function that takes variable arguements (i.e. has char * fmt and '...' as two of its parameters), finds out how many bytes the resulting string formed by the arguement list will be, allocates that many bytes, then calls vsprintf or a similar function to actually write the string.

it says on msdn that there is the _vscprintf function, which returns the number of bytes (which is exactly what i want), but my compiler (msvc 6.0) can't find the function even though i've included the headers it says to. in fact, a search for the text "_vscprintf" in all files under the "...\Visual Studio\" path comes up empty, so not only is it not defined anywhere in my libraries, the string '_vscprintf' does not appear anywhere in my libraries.

i also found during research that the vsnprintf function is supposed to take a byte count as the buffer size, and return the byte amount that would have been written if the buffer were large enough. so, u can call it once with a NULL buffer and 0 count, then call it again after allocating the return value of the first call. but, the implementation of this on my windows machine returns -1 if the string is not long enough (so, i cant use this function on windows for my purporses).

anyone know how to do this, preferablably as portable as possible although i'm fine with doing a [#ifdef WIN32 ... #else ... #endif] if there's no other way.
 
> it says on msdn that there is the _vscprintf function
Sadly, this is only in VC++.NET

> the implementation of this on my windows machine returns -1 if the string is not long enough
Blame the ANSI-C committee. They chose an interface which retained compatibilty with everything else printf (ie, return negative on error), rather than something which would be useful.

The GNU-libc version of this function (prior to C99) was capable of telling you how many chars would have been required, if the space was insufficient. So the first call would in effect inquire about the space needed, then you would call malloc to allocate that, and call it again to do the actual conversions.

> anyone know how to do this, preferablably as portable as possible
It's easy to do it portably, it's just not the most wonderfully efficient way of doing it - since you have to keep guessing at the space you need.

My approach would be this:
99% of the results would be fairly short - less than 1K for sake of argument. My first call would use a buffer of this size.
In the 1% of cases where this fails, call vsnprintf in a loop along side a call to realloc, which allocates double the amount of memory on each iteration (2K, 4K, 8K etc) up to some guard limit.

Although its not hard to create your own simple version of vscprintf - %c counts 1, %dxiu etc count 11 and call strlen() for each %s, taking into account all the field width and precision modifiers would take some time.

--
 
thanks for the reply. i was afraid i'd have to roll my own.
 
Salem's approach with buffer size matching process is OK, then _vsnprintf() is a proper tool. This function is "conditionally-portable", it came from Unix System V.
100% portable solution: vfprintf() to temp file then check its size - it seems that's all (in Std C;)...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top