 |
|
 |
|
08-27-04 11:21 PM
Hello
I want to write function which will return me string
formatted by sprintf:
char* sprintf_container(const char *format,...){
str=malloc('what size' ??);
sprintf(str,format,???);
return str;
}
But how should i pass other parameters to sprintf ?
Thanx
Michal
[ Post a follow-up to this message ]
|
|
|
 |
|
|
08-27-04 11:21 PM
vertigo <none@microsoft.com> writes:
> Hello
> I want to write function which will return me string
> formatted by sprintf:
>
> char* sprintf_container(const char *format,...){
> str=malloc('what size' ??);
> sprintf(str,format,???);
> return str;
> }
>
> But how should i pass other parameters to sprintf ?
man vsprintf
--
Måns Rullgård
mru@mru.ath.cx
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
08-27-04 11:21 PM
vertigo <none@microsoft.com> writes:
> Hello
> I want to write function which will return me string
> formatted by sprintf:
>
> char* sprintf_container(const char *format,...){
> str=malloc('what size' ??);
> sprintf(str,format,???);
> return str;
> }
>
> But how should i pass other parameters to sprintf ?
You won't be using sprintf.
You should really, as always, read the manual page for sprintf: man sprintf
Where you'll notice vsnprintf.
Then you'll refer to the stdarg man page: man stdarg
--
__Pascal Bourguignon__ http://www.informatimago.com/
Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
 |  |  |  |  |
 |
 |
|
Jens.Toerring@physik.fu-berlin.de |
|
|
 |
 |


 |
 |
 |
|  |  |  |  |
|
08-27-04 11:21 PM
vertigo <none@microsoft.com> wrote:
> I want to write function which will return me string
> formatted by sprintf:
> char* sprintf_container(const char *format,...){
> str=malloc('what size' ??);
> sprintf(str,format,???);
> return str;
> }
Here's what I use for that:
#define SPRINTF_CONTAINER_TRY_LENGTH 128
char *sprintf_container( const char *fmt, ... )
{
char *c = NULL, *new_c;
size_t len = SPRINTF_CONTAINER_TRY_LENGTH;
va_list ap;
int wr;
while ( 1 )
{
if ( ( new_c = realloc( c, len ) ) == NULL )
{
free( c );
return NULL;
}
c = new_c;
va_start( ap, fmt );
wr = vsnprintf( c, len, fmt, ap );
va_end( ap );
if ( wr < 0 ) /* indicates not enough space with older glibc */
{
len *= 2;
continue;
}
if ( ( size_t ) wr + 1 > len ) /* new glibc returns the number of */
{ /* chars needed, not counting the */
len = wr + 1; /* trailing '\0' */
continue;
}
break;
}
/* Trim the string to the number of required characters */
if ( ( size_t ) wr + 1 < len )
{
if ( ( new_c = realloc( c, ( size_t ) wr + 1 ) ) == NULL )
return c;
else
c = new_c;
}
return c;
}
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
08-27-04 11:21 PM
On Fri, 27 Aug 2004 11:48:53 +0200
vertigo <none@microsoft.com> wrote:
> Hello
> I want to write function which will return me string
> formatted by sprintf:
>
> char* sprintf_container(const char *format,...){
> str=malloc('what size' ??);
> sprintf(str,format,???);
> return str;
> }
>
> But how should i pass other parameters to sprintf ?
>
> Thanx
> Michal
>
or you could use asprintf(). not standard C, but it is portable on linux an
d
bsd.
--
-sean
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
08-27-04 11:21 PM
Jens.Toerring@physik.fu-berlin.de wrote:
> vertigo <none@microsoft.com> wrote:
>
>
>
>
>
> Here's what I use for that:
>
> #define SPRINTF_CONTAINER_TRY_LENGTH 128
>
> char *sprintf_container( const char *fmt, ... )
> {
> char *c = NULL, *new_c;
> size_t len = SPRINTF_CONTAINER_TRY_LENGTH;
> va_list ap;
> int wr;
>
>
> while ( 1 )
> {
> if ( ( new_c = realloc( c, len ) ) == NULL )
> {
> free( c );
if realloc() fails, your program will crash on most free() implementations.
--
Lev Walkin
vlm@lionet.info
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
 |
|
 |
|
08-27-04 11:21 PM
Jens.Toerring@physik.fu-berlin.de wrote:
> Lev Walkin <vlm@lionet.info> wrote:
>
>
>
>
>
> In that case 'c' should be either a value returned from a previous call
> of alloc(), in which case I don't see a problem, or NULL, and then it
> should be a no-op (as far as I can see from the C89 standard). Or am I
> overlooking something?
you're overlooking the real world.
however, I've just checked, and indeed it seem to have improved on newer
systems. however, I remember this being a common inconvenience on the older
ones, so it's better not to rely on it.
--
Lev Walkin
vlm@lionet.info
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
08-27-04 11:21 PM
William Ahern wrote:
> Lev Walkin <vlm@lionet.info> wrote:
>
>
>
> Care to elaborate? I'm quite surprised to hear that.
The code checks for old behavior of vsnprintf(), which may return -1 instead
of the "estimated" numer of characters. But the code does not go further and
check if the c is non-0 before free()'ing. See my other follow-up on the C89
quote.
--
Lev Walkin
vlm@lionet.info
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 09:39 AM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|