C fprintf() function : C Reference




fprintf : sends formatted output to a stream


Syntax :

int fprintf (FILE *stream, const char *format [, argument, ...]);

What it Does ?

  1. sends formatted output to a stream .
  2. Write Formatted Output to Stream. 

How ?
There are different ways of Writing the String to the File .Some of the verities of fprintf are shown below – 
Way 1 : Writing Two Words Separated by Spaces

void main()
{
FILE *fp;
fp = fopen("op.txt","w"); // Open File in Write Mode
fprintf(fp,"Hello World");
}

Way 2 : Writing Formatted Output on the File

void main()
{
FILE *fp;
int a,b,c;
a = 10;
b = 20;
fp = fopen("op.txt","w"); // Open File in Write Mode
c = a + b ;
fprintf(fp,"nSum of %d and %d is %d",a,b,c);
}

Output :
“Sum of 10 and 20 is 30” is written onto File


Steps to Understand the Working of Printf !!!!

Step1 : 

From the above Line of Code Replace

fprintf(fp,"Hello World");

By the following Line

printf("Hello World");

Step 2 :

Guess What will be the Output of the Program ?

  1. Here Output will be “Hello World” on the Screen 
  2. fprintf is Similar to the printf but it just write the Output on the File instead of Writing it on the Screen.


    Bookmark & Share