vfprintf() — Format and print data to stream

Standards

Standards / Extensions C or C++ Dependencies

ISO C
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3
Language Environment

both  

Format

#include <stdarg.h>
#include <stdio.h>

int vfprintf(FILE *  __restrict__stream, 
             const char *  __restrict__format, va_list arg_ptr);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <stdio.h>

int vfprintf_unlocked(FILE *  __restrict__stream, 
                      const char *  __restrict__format, va_list arg_ptr);

General description

The vfprintf() function is similar to fprintf(), except that arg_ptr points to a list of arguments whose number can vary from call to call in the program. These arguments should be initialized by va_start() for each call. In contrast, fprintf() can have a list of arguments, but the number of arguments in that list is fixed when you compile the program. For a specification of the format string, see fprintf(), printf(), sprintf() — Format and write data.

vfprintf() is not supported for files opened with type=record or type=blocked.

vfprintf() has the same restriction as any write operation for a read immediately following a write or a write immediately following a read. Between a write and a subsequent read, there must be an intervening flush or reposition. Between a read and a subsequent write, there must also be an intervening flush or reposition unless an EOF has been reached.

vfprintf_unlocked() is functionally equivalent to vfprintf() with the exception that it is not thread-safe. This function can safely be used in a multithreaded application if and only if it is called while the invoking thread owns the (FILE*) object, as is the case after a successful call to either the flockfile() or ftrylockfile() function.

Returned value

If successful, vfprintf() returns the number of characters written to stream.

If unsuccessful, vfprintf() returns a negative value.
Note: In contrast to some UNIX-based implementations of the C language, the z/OS® XL C/C++ implementation of the vprintf() family increments the pointer to the variable arguments list. To control whether the pointer to the argument is incremented, call the va_end macro after each call to vsprintf().

Example

CELEBV03
⁄* CELEBV03                                      

   This example prints out a variable number of strings to the                  
   file myfile.dat, using &vfprt..                                              
                                                                                
 *⁄                                                                             
#include <stdarg.h>                                                             
#include <stdio.h>                                                              
                                                                                
void vout(FILE *stream, char *fmt, ...);                                        
char fmt1 [] = "%s  %s  %s\n";                                                  
                                                                                
int main(void)                                                                  
{                                                                               
   FILE *stream;                                                                
   stream = fopen("myfile.dat", "w");                                           
                                                                                
   vout(stream, fmt1, "Sat", "Sun", "Mon");                                     
}                                                                               
                                                                                
void vout(FILE *stream, char *fmt, ...)                                         
                                                                                
{                                                                               
   va_list arg_ptr;                                                             
                                                                                
   va_start(arg_ptr, fmt);                                                      
   vfprintf(stream, fmt, arg_ptr);                                              
   va_end(arg_ptr);                                                             
}                                                                               
                                                                                
Output:
Sat  Sun  Mon

Related information