C Program for using file handling function : fprintf()

fprintf() Function In File Handling in C
 Programming.


                                             fprintf() function is used to print multistring variables into the File.


with the help of this function we can print an integer value as well as string value or other type of values  simultaneously.


So we do  not need to use printf() function many times.


By using fprintf() function just for one time we can print all values into the file.


Let us understand the Syntax of fprintf() function....!


fprintf (file-Pointer , "Format-Strings" , Var - List );

for example : -


fprintf(fp,"%d,%s,%d,%f",rno,sname,class,marks);


Now , let us see C program for use of fprintf() function .


#include<stdio.h>

#include<stdlib.h>

int main()

{

    FILE *fp;

    int rno;

    char sname[20];

    float marks;

    fp = fopen("D:\\text\\fprintf.txt","w+");

    if(fp == NULL)

    {

        printf("Error : File does not found!!");

        exit(0);

    }

    else{

        printf("\n File location found!!");

        printf("\n Enter student roll number : ");

        scanf("%d",&rno);

        printf("\n Enter student name : ");

        scanf("%s",sname);

        printf("\n Enter marks of student : ");

        scanf("%f",&marks);

        fprintf(fp,"Roll no. : %d\nStudent Name : %s\nMarks : %.2f",rno,sname,marks);

        printf("\n One record is inserted Sucessfully.");

    }

}


As you can see in above program fprintf() function is used which print roll number ,student name and it's marks into fprintf.txt file. so one function fprintf is enough to print all these values, so this is the very vital use of this function.


Input && Output










Post a Comment

0 Comments