C examples for stdio.h:vsnprintf
function
<cstdio> <stdio.h>
Write formatted data from variable argument list to sized buffer
int vsnprintf (char * s, size_t n, const char * format, va_list arg );
Parameter | Description |
---|---|
s | resulting C-string. |
n | Maximum number of bytes to be used in the buffer. |
format | a format string |
arg | a variable arguments list |
On success, the number of characters written.
On error, a negative number is returned.
#include <stdio.h> #include <stdarg.h> void PrintFError ( const char * format, ... ) { char buffer[256]; va_list args;/* w ww . j av a 2 s.c o m*/ va_start (args, format); vsnprintf (buffer,256,format, args); perror (buffer); va_end (args); } int main () { FILE * pFile; char szFileName[]="main.cpp"; pFile = fopen (szFileName,"r"); if (pFile == NULL){ PrintFError ("Error opening '%s'",szFileName); return -1; } fclose (pFile); return 0; }