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....!
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.");
}
}
0 Comments