vfprintf() - I/O Function

How is "vfprintf()" used in C++?

Explanation

vfprintf() is an I/O function that writes the contents of the format string to an array, with a pointer to a list of arguments replacing the argument list. This function returns the total number of characters, but on failure a negative value is returned.

Syntax:


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

The following table lists the type specifier used with vfprintf()
Type Description
c Character
d or i Signed decimal integer
e Scientific notation using e character
E Scientific notation using E character
f Decimal floating point
g Use the shorter of %e or %f
G Use the shorter of %E or %f
o Signed octal
s String of characters
u Unsigned decimal integer
x Unsigned hexadecimal integer
X Unsigned hexadecimal integer(capital)
p Pointer address
% A % followed by another % character will write % to the stream.

Example :



#include <stdio.h>
#include <stdarg.h>
void Form (FILE *str, char * format, ...)
{
va_list args;
va_start (args, format);
vfprintf (str,format, args);
va_end (args);
}
int main ()
{
FILE * str;
str = fopen ("vfprintf.txt","w");
Form (str,"With only one argumet %f \n",1.0);
Form (str,"With two arguments %d , %s.\n",2,"arguments");
fclose(str);
return 0;
}

Result:vfprintf.txt



With one floating point argumet:: 1.000000
With decimal, string argument:: 2 , asddasd.

In the above example the contents are written to a stream "str" with single, multiple arguments.

C++ Tutorial


Ask Questions

Ask Question