C programming processing va_args using sprintf? -
i trying learn how va_args work, want merge list of args format string inside of function im making. way im doing results in jiberish. there way let me avoid having write massive processing function?
va_start( listpointer, msg ); #ifdef debug fprintf(stderr, msg, listpointer); #endif str = (char *)calloc(sizeof(max_line_size), sizeof(char)); if(str == null){ fprintf(stderr, "out of memory exception"); return; } snprintf(str, max_line_size, listpointer); printf("testing: %s\n", str); ret = write(logger, str, strlen(str)); if(ret == -1){ fprintf(stderr, "could not write file error: %s\n", strerror(errno)); fprintf(stderr, "cannot continue error logging"); } va_end( listpointer );
use vsprintf or vsnprintf function. rather taking list of parameters takes va_list parameters.
you call as:
vsnprintf(str, max_line_size, listpointer);
Comments
Post a Comment