vfprintf()

Writes formatted output to a stream.

  #include <stdarg.h>
  
  #include <stdio.h>
  
  int vfprintf(FILE *stream, const char *format, va_list arg);    
Parameter

stream

A pointer to a file stream.

format

A format string.

arg

An argument list.

Remarks

The vfprintf() function works identically to the fprintf() function. Instead of the variable list of arguments that can be passed to fprintf(), vfprintf() accepts its arguments in the array arg of type va_list which must have been initialized by the va_start() macro from the stdarg.h header file. The vfprintf() does not invoke the va_end macro.

vfprintf() returns the number of characters written or EOF if it failed.

This facility may not be available on configurations of the EWL that run on platformswithout file systems.

Listing: Example of vfprintf() Usage

#include <stdio.h>

#include <stdlib.h>

#include <stdarg.h>

int fpr(FILE *, char *, ...);

int main(void)

{

FILE *f;

static char name[] = "foo";

int a = 56, result;

double x = 483.582;

// create a new file for output

if (( f = fopen(name, "w")) == NULL) {

printf("Can't open %s.\n", name);

exit(1);

}

// format and output a variable number of arguments

// to the file

result = fpr(f, "%10s %4.4f %-10d\n", name, x, a);

// close the file

fclose(f);

return 0;

}

// fpr() formats and outputs a variable

// number of arguments to a stream using

// the vfprintf() function

int fpr(FILE *stream, char *format, ...)

{

va_list args;

int retval;

va_start(args, format); // prepare the arguments

retval = vfprintf(stream, format, args);

// output them

va_end(args); // clean the stack

return retval;

}

Output to file foo:

foo 483.5820 56