vsprintf - C stdio.h

C examples for stdio.h:vsprintf

Type

function

From


<cstdio>
<stdio.h>

Description

Write formatted data from variable argument list to string

Prototype

int vsprintf (char * s, const char * format, va_list arg );

Parameters

ParameterDescription
s the resulting C-string.
format a format string
arg a variable arguments list

Return Value

On success, the total number of characters written is returned. On failure, a negative number is returned.

Example

In this example, if the file main.cpp does not exist, perror is called to show an error message similar to:

Demo Code


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

Related Tutorials