C programming stdio.h function - int vfprintf(FILE *stream, const char *format, va_list arg)

w‮igi.ww‬ftidea.com

The vfprintf function in the stdio.h library of C programming language is used to write formatted output to a file stream, given a va_list of arguments. It has the following syntax:

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

The vfprintf function is similar to the fprintf function, but instead of taking a variable number of arguments, it takes a va_list argument that contains a list of the values to be printed.

The format argument is a string that specifies the format of the output, and the arg argument is a va_list that contains the values to be substituted into the format string.

Here's an example that uses vfprintf to format a string and write it to a file:

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

void write_to_file(FILE *stream, const char *format, ...) {
   va_list args;
   va_start(args, format);
   vfprintf(stream, format, args);
   va_end(args);
}

int main() {
   FILE *f = fopen("output.txt", "w");
   write_to_file(f, "%s %d %f\n", "Hello", 42, 3.14);
   fclose(f);
   return 0;
}

This will write the string "Hello 42 3.140000" to a file named "output.txt". The write_to_file function takes a FILE pointer, a format string, and a variable number of arguments. It uses va_start, vfprintf, and va_end to write the formatted output to the file. The vfprintf function takes the FILE pointer, the format string, and the va_list of arguments.