C examples for stdio.h:vsprintf
function
<cstdio> <stdio.h>
Write formatted data from variable argument list to string
int vsprintf (char * s, const char * format, va_list arg );
Parameter | Description |
---|---|
s | the resulting C-string. |
format | a format string |
arg | a variable arguments list |
On success, the total number of characters written is returned. On failure, a negative number is returned.
In this example, if the file main.cpp does not exist, perror is called to show an error message similar to:
#include <stdio.h> #include <stdarg.h> void PrintFError ( const char * format, ... ) { char buffer[256]; va_list args;/*from w ww . j a v a 2s .c o m*/ va_start (args, format); vsprintf (buffer,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; }