fprintf function is another important function in file handling in c.
In this tutorial we are going to learn fprintf() function,how to use fprintf() function,why fprintf() function is used.
As we know different file handling function to write character,string and integer type of data to file.but one file can contain different type of data so to write mixed type of data to file fprintf is used.
fprintf function :- This function is used to write mixed (different)type of data to the File.
fprintf Syntax in c
fprintf(FILE *fp,”format _seocifier”, variable list);
In above syntax we can see fprintf function has 3 parameter which are as follows
- FILE *fp :- First parameter is file pointer which is pointer to file which holds address of file.
- Format specifier :- second parameter is format specifier which contains different format specifier for different type (example:- for integer %d ,for float %f )
- Variable list :- This is list of variable which we want to write to file.
Syntax for fprintf is same as printf except fprintf accept first parameter file pointer which point to file.
fprintf function is same as printf function, fprintf function writes data to file whereas printf writes data to standard output screen.
fscanf function is used to read mixed type of data from file.
Program to demonstrate fprintf function
printf ("\nopening file in write mode");
fp=fopen("technical.txt","w");. //Opening file in write mode
//Take input from user
printf("\nEnter no ");
printf ("\n Enter name");
//Writing no and name to file
printf ("\nwritten data using fprintf is successfully");
- In above program we write the integer and string data to file .
- First we open file in write mode.fopen(“technical.txt”,”w”) opens the file in write mode.
- Take input from user which want to write to file i.e no and name.
- Next write fprintf (fp,”%d.%s”,no,name) which writes the integer type no and string name to the file.
- Now you can see data stored to file.
opening file in write mode
written data using fprintf is successfully