Better code:
Code:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#ifdef __WINNT__
# include <stdarg.h>
// Windows libc don't have asprintf function...
int asprintf ( char **, char *, ... );
#endif
int main ( void )
{
int NumLinha = 10, QtdCaracteresLinha = 11;
char Buffer[] = "Giant Text";
char *juntando;
// I'll assume NumLinha can be 2łą-1 (+2147483647), so 10 chars long (plus '\0');
if ( asprintf ( &juntando, "Linha %d (%d) %s\n", NumLinha, QtdCaracteresLinha, buffer ) == -1 )
{
// Error!
fputs ( "Error alocating memory!\n", stderr );
return EXIT_FAILURE;
}
puts ( juntando ); // shows buffer pointed by 'juntando'...
// Free the buffer.
free ( juntando );
}
#ifdef __WINNT__
int asprintf ( char **buffptr, char *fmt, ... )
{
int size;
char tmp[2];
va_list argp;
/* Gets the total buffer size... */
va_start ( argp, fmt );
size = vsnprintf ( tmp, 1, fmt, argp );
if ( size < 1 )
{
*s = NULL;
return -1;
}
va_end ( argp );
/* Alocates buffer. */
if ( ( *s = ( char * ) malloc ( size + 1 ) ) == NULL )
return -1;
/* Finally, fills the buffer. */
va_start ( argp, fmt );
vsnprintf ( *s, size + 1, fmt, argp );
va_end ( argp );
return size;
}
#endif