Article View: es.comp.os.linux.programacion
Article #10165Re: stdarg.h
From: Santiago Romero
Date: Tue, 02 Apr 2002 09:40
Date: Tue, 02 Apr 2002 09:40
44 lines
2584 bytes
2584 bytes
El 27 Mar 2002 20:09:57 +0100, Jes�s Carrete Monta�a escribi�:
Del man vsprintf:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
char *
construye_mensaje(const char *fmt, ...) {
/* Suponemos que no necesitamos m�s de 100 bytes. */
int n, size = 100;
char *p;
va_list ap;
if ((p = malloc (size)) == NULL)
return NULL;
while (1) {
/* Intenta imprimir en el espacio reservado. */
va_start(ap, fmt);
n = vsnprintf (p, size, fmt, ap);
va_end(ap);
/* Si ha funcionado, devuelve la cadena. */
if (n > -1 && n < size)
return p;
/* Si no, int�ntalo de nuevo con m�s espacio. */
if (n > -1) /* glibc 2.1 */
size = n+1; /* exactamente lo que se necesita */
else /* glibc 2.0 */
size *= 2; /* el doble del tama�o anterior*/
if ((p = realloc (p, size)) == NULL)
return NULL;
}
}
saludos.
--
Santiago Romero
Departamento de Sistemas
sromero@servicom2000.com
Av. Primado Reig 189, entlo
46020 Valencia - Spain
Telf. (+34) 96 332 12 00
Fax. (+34) 96 332 12 01
http://www.servicom2000.com
Technical Details
Message-ID:
<slrnaaiv56.ub0.sromero@compiler.servicom2000>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!fu-berlin.de!uni-berlin.de!top.servicom2000.COM!not-for-mailReferences:
<87d6xpket6.fsf@meltdown.ion>Show Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII X-Google-Thread: f86c8,f5da4abf70825f48 X-Google-Attributes: gidf86c8,public X-Google-ArrivalTime: 2002-04-02 01:55:30 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!fu-berlin.de!uni-berlin.de!top.servicom2000.COM!not-for-mail From: Santiago Romero <sromero@servicom2000.com> Newsgroups: es.comp.os.linux.programacion Subject: Re: stdarg.h Date: Tue, 2 Apr 2002 11:40:54 +0200 Lines: 45 Message-ID: <slrnaaiv56.ub0.sromero@compiler.servicom2000> References: <87d6xpket6.fsf@meltdown.ion> Reply-To: sromero@servicom2000.com NNTP-Posting-Host: top.servicom2000.com (212.101.64.178) Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: fu-berlin.de 1017741328 28620863 212.101.64.178 (16 [39726]) X-Orig-Path: 127.0.0.1!nobody User-Agent: slrn/0.9.7.3 (Linux)