VSNPRINTF printing variable-parameter log

Source: Internet
Author: User

Vsnprintf,c one of the language library functions, which belongs to a mutable parameter. Used to print data, data format user customizations to a string.

Header file:
#include <stdarg.h>
function declaration:
int_vsnprintf (CHAR*STR,SIZE_TSIZE,CONSTCHAR*FORMAT,VA_LISTAP);
Parameter description:
Char *str [out], storing the generated formatted string here.
size_t size [in], the maximum number of bytes that STR can accept to prevent an array from being out of bounds.
const char *format [in], specifying the output format string, which determines the type, number, and order of the mutable arguments you need to provide.
Va_list ap [in], va_list variable. Va:variable-argument: Variable Parameters
function function: format the variable parameter to an array of characters.
The usage is similar to vsprintf, but the size limit is added to prevent memory overflow (size is the amount of storage space referred to by STR).
return value: execution succeeds, returns the number of characters written to the character array str (does not contain a terminator), the maximum size is not exceeded, the execution fails, a negative value is returned, and the errno is reset. [1]
Note:
Linux environment is: vsnprintf
VC6 Environment is: _vsnprintf

---

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

static const int max_log_length = 4*1024;

static void Print_log (const char * format, ...)
{
    va_list args;
    Va_start (args, format);

    Char Buf[max_log_length];

    vsnprintf (buf, max_log_length-3, format, args);
    strcat (buf, "\ n");
    fprintf (stdout, "log:%s", buf);
    Fflush (stdout);
    Va_end (args);
}


int main () {
	Print_log ("%d%s", 111000, "test");

	return 0;
}

-------

Reference: http://stackoverflow.com/questions/1056411/how-to-pass-variable-number-of-arguments-to-printf-sprintf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.