vfprintf() — Format and print data to stream
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C |
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.
Example
/* 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);
}
Sat Sun Mon