fprintf Function in C Programming

fprintf Function

Declaration: fprintf (FILE *stream, const char *format,.....);

The fprintf() function outputs the values of the arguments that makes up the argument list as specified in the format string to the stream pointed to by stream. The operations of the format control string and command are identical to those in printf().

The following program uses the fprintf() and fscanf() function to prepare the results of 'n' students.

#include<stdio.h>           
int main() 
                        { 
                        int regno,m[5],tot,I,j,k,n; 
                        char name[15]; 
                        float avg; 
                        FILE * fptr; 
                        if((fptr = fopen("MARK","w"))==NULL) 
                                 { 
                                  printf("Cannot open file\n"); 
                                  exit(1); 
                                 } 
                       clrscr(); 
                       printf("How many students?"); 
                       scanf("%d",&n); 
                        for(i=0;i<n;i++)
                               { 
                               clrscr(); 
                                                             
        printf("Enter the details of Student-%d\n\n",i+1); 
                               printf("Name?"); 
                               scanf(%[^\n],name); 
                               printf("Register Number?"); 
                               scanf(%d,&regno); 
                              for(tot=0,j=0;j<5;++j) 
                                 { 
                                 printf("Mark in 
        paper-%d?",j+1); 
                                 scanf(%d,&m[j]); 
                                 tot+=m[j]; 
                                } 
                             avg=(float)tot/5.0; 
                                             
        fprintf(fptr,"%15s%d%d%d%d%d%d%d%f\n",name,regno,m[0], m[1],m[2], m[3], 
        m[4],tot,avg); 
               } 
                clrscr(); 
                fclose(fptr); 
                if((fptr = fopen("MARK","r"))==NULL) 
                          { 
                          printf("Cannot open file\n"); 
                          exit(1); 
                             } 
         printf("NAME\t\tReg.No\TM1 M2 M3 M4 M5 TOTAL AVERAGE RES"); 
            for(i=0;i<n;i++) 
                     { 
         fscanf(fptr,"%-15s%d%d%d%d%d%d%d%f",&name,&regno,&m[0], 
        &m[1],&m[2], &m[3], &m[4],&tot,&avg); 
         printf("%15s%d%d%d%d%d%d%d%f\n",name,regno,m[0], m[1],m[2], 
        m[3], m[4],tot,avg); 
         if(avg>35.0) 
         printf("Passed"); 
         else 
         printf("Failed"); 
         } 
         fclose(fptr); 
         getch(); 
         return 0; 
         } 

 

Ready to get started?

Ready to embark on your journey into the world of C programming? Our comprehensive course provides the perfect starting point for learners of all levels. With engaging lessons, hands-on exercises, and expert guidance, you'll gain the skills and confidence needed to excel in this fundamental programming language. Let's dive in and unlock the endless possibilities of C programming together!