vsnprintf - C stdio.h

C examples for stdio.h:vsnprintf

Type

function

From


<cstdio>
<stdio.h>

Description

Write formatted data from variable argument list to sized buffer

Prototype

int vsnprintf (char * s, size_t n, const char * format, va_list arg );

Parameters

ParameterDescription
s resulting C-string.
n Maximum number of bytes to be used in the buffer.
format a format string
arg a variable arguments list

Return Value

On success, the number of characters written.

On error, a negative number is returned.

Example

Demo Code


#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;
}

Related Tutorials