Introduction to fprintf() in C
In the C programming language, a library function fprintf which is also known as format print function sends output that is formatted to a stream. Even though it prints the message, it is not possible on stdout console. It is almost similar to normal printf() function except in the fact that it writes data into the file. Moreover, an additional argument is also present in fprintf() function. It is a file pointer that points to the file where the formatted output will be written. The total count of characters that writes to the file will be returned if it is a success. An EOF will be returned if it is failed.
Syntax and Parameters
Below is the syntax of the function fprintf() in the C programming language.
int fprintf(FILE *stream, const char *format, ...)
Parameters:
- The stream which is the pointer to a file object that finds the stream.
- The format is the C string which consists of the text that to be written to the stream. Embedded format tags are also present which can be replaced by the values mentioned in subsequently added arguments and formatted. The prototype of format tags is %[flags][width][.precision][length]specifier.
Return Value:
The total count of characters that writes to the file will be returned if it is a success. An EOF will be returned if it is failed.
The format can be:
1. %d: An integer will be displayed
Example: 9
2. %f: A floating point number will be displayed with a fixed decimal format.
Example: 9.000050
3. %.1f: A floating point number will be displayed with one number after the decimal.
Example: 9.0
4. %e: A floating point number will be displayed in exponential.
Example: 9.00045e+1
5. %g: A floating point number will be displayed with a fixed decimal format or exponential based on the number size.
Example: 9.4
Required Header Format:
The required header format in C programming language for the function fprintf is:
#include <stdio.h>
How fprintf() Function Works in C?
- First, initialize variables based on your requirement.
- Open the text file in the specified location in write mode using a file pointer.
- If the file pointer is null, print an error message.
- If the file pointer is not null, execute the commands based in the requirement.
- Open the file to check whether the code runs successfully and the output is available in it.
Examples of fprintf() in C
Let us see some sample programs on fprintf() function.
Example #1
C program to Print Names in a File
Code:
#include<stdio.h>
int main()
{
//initialize two integer variables i and n
int i, n=1;
//initialize one character variable
char s[100];
//open the text file in write mode
FILE *filepntr = fopen("C:\\Users\\SCRC_Laptop\\Documents\\C\\sample.txt", "w");
//if file pointer is null, print the statement
if (filepntr == NULL)
{
printf("Sorry. . The file you are trying to open donot exist . . .");
return 0;
}
//if file pointer is not null, execute the for loop
for (i=0; i<n; i++)
{
puts("Enter user name");
gets(s);
fprintf(filepntr,"%d.%s\n", i, s);
}
fclose(filepntr);
return 0;
}
Output:
First, initialize character variable s and two integer variables i and n where n=1. Then, open the text file in the specified location write mode. If the file pointer is null, print “Sorry. The file you are trying to open do not exist . . .”. If the file pointer is not null, execute the for loop that checks whether i<n. As n=1 the loop will execute only one time. Then, the user will be asked to input a user name. This user name will be stored in the file mentioned. It can be seen that on executing the code, a user name will be asked to input. When the file is opened, the name with the index will be displayed as shown below.
Suppose the value of n is changed to 3. Then the user name will be asked three times as depicted in the below figure.
The data in the file will be as shown below.0,1,2 and 3 are the values of i.
Example #2
C Program to Print Student Details in A File.
Code:
#include <stdio.h>
int main()
{
//initialise a file pointer
FILE *filepntr;
//
int rollnum;
char studentname[30];
float mark;
//create a file if not already present
filepntr = fopen("studentinfo.txt", "w+");
if (filepntr == NULL)
{
printf("The file you are trying to open does not exist. . . \n");
return 0;
}
printf("Enter the student roll number : \n");
scanf("%d", &rollnum);
fprintf(filepntr, "roll number= %d\n", rollnum);
printf("Enter the student name: \n");
scanf("%s", studentname);
fprintf(filepntr, "student name= %s\n", studentname);
printf("Enter the mark\n");
scanf("%f", &mark);
fprintf(filepntr, "mark= %.3f\n", mark);
fclose(filepntr); }
Output:
In this program, first, initialize a file pointer *filepntr. Then, initialize rollnum, student name, mark. Then create a file if not already present. If the file pointer is null, print an error message. Once this is completed, enter the code for inputting the student roll number, student name, and the mark. On executing the code, the user will be asked to input these three values. On successful submission, a file will be created in the folder. The filename of the file created is mentioned in our file pointer.
On opening the file, the details that gave as input will be displayed inside it.
Example #3
C Program to Print Student Details in a File with Value of i.
#include <stdio.h>
int main()
{
FILE *filepntr;
int i, n=1;
int rollnum;
char studentname[30];
float mark;
filepntr = fopen("studentinfo.txt", "w+");
if (filepntr == NULL)
{
printf("The file you are trying to open does not exist. . . \n");
return 0;
}
for (i=0; i<n; i++)
{
fprintf(filepntr,"%d\n", i);
printf("Enter the student roll number : \n");
scanf("%d", &rollnum);
fprintf(filepntr, "roll number= %d\n", rollnum);
printf("Enter the student name: \n");
scanf("%s", studentname);
fprintf(filepntr, "student name= %s\n", studentname);
printf("Enter the mark\n");
scanf("%f", &mark);
fprintf(filepntr, "mark= %.3f\n", mark);
}
fclose(filepntr);
return 0;
}
Output:
This program prints the index value along with the details of the student inside the file.
Recommended Articles
This is a guide to fprintf() in C. Here we also discuss the definition and how fprintf() function work in C along with different examples and its code implementation. You may also have a look at the following articles to learn more –
3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses