Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

vfprintf - Formatted output to a file

&pagelevel(4)&pagelevel

Definition   

#include <stdio.h>

int vfprintf(FILE *fp, const char *format, va_list arg);

vfprintf is similar to the fprintf function. In contrast to fprintf, vfprintf enables arguments to be output whose number and data types are not known at compilation time.

vfprintf is used within functions to which the caller can pass a different format string and different arguments for output. The formal parameter list of the function definition provides for a format string format and a variable argument list ", ..." for this purpose.
format is a format string as described under printf with ANSI functionality (see printf).

vfprintf steps through an argument list arg with internal va_arg calls and writes the arguments according to format string format to the file with file pointer fp. Variable argument list arg must be initialized with the va_start macro before vfprintf is called.

Return val.

Number of characters output



if successful.


Integer< 0

if an error occurs.                                                          

Notes

vfprintf always starts with the first argument in the variable argument list. It is possible to start output from any particular argument by issuing the appropriate number of va_arg calls before calling the vfprintf function. Each va_arg call advances the position in the argument list by one argument.

vfprintf does not call the va_end macro. Since vfprintf uses the va_arg macro, the value of arg is undefined on return.

The following applies in the case of text files with SAM access mode and variable record length for which a maximum record length is also specified: When the specification split=no was entered for fopen, records which are longer than the maximum record length are truncated to the maximum record length when they are written. By default or with the specification split=yes, these records are split into multiple records. If a record has recisely the maximum record length, a record of the length zero is written after it.

Example

In the following program extract the vfprintf function outputs different types of information each time the error routine error is called.

#include <stdarg.h>
#include <stdio.h>
void error(char *f, ...);
int main(void)
{
  .
  .
  char *weight = "WARNING";
  int num = 20;
  error("Error class: %s, Number: %d\n", weight, num);
  .
  .
  error("No error\n");
  .
  .
}
void error(char *format, ...)
{
  va_list arg;
  va_start(arg, format);
  vfprintf(stderr, format, arg);
  va_end (arg);
}

See also

vprintf, vsprintf, vsnprintf