logo
fscanf and fprintf functions In C Language
The fprintf and fscanf functions are identical to printf and scanf functions except that they work on files. The first argument of these functions is a file pointer which specifies the file to be used. The general form of fprintf is 

                  fprintf(fp, "controlstring", list); 

Where fp is a file pointer associated with a file that has been opened for writing. The control string is file output specifications list may include variable, constant and string. 

                  fprintf(fl ,”%s%d%f”,name,age,7 .5); 

Here, name is an array variable of type char and age is an int variable the general format of fscanf is 

                  fscanf(fp, "controlstring" ,list); 

This statement would cause the reading of items in the control string.
  Program : The following program is an example of fscanf() and fprintf()
#include<stdio.h>
#include<conio.h>
struct emp
{
   char name[10];
   int age;
};

main()
{
   struct emp e;
   FILE *p,*q;
   p = fopen(“one.txt”, “a”);
   q = fopen(“one.txt”, “r”);



   printf(“Enter Name and Age”);
   scanf(“%s %d”, e.name, &e.age);
   fprintf(p,{“%s %d”, e.name, e.age);
   fclose(p);
   do
   {
      fscanf(q,”%s %d”, e.name, e.age);
      printf(“%s %d”, e.name, e.age);
   }
   while(!eof(q));
   getch();
}
Output :

Enter Name and Age: Ramana 27
Ramana 27