Updated May 18, 2023
Introduction to fprintf() in C
In the C programming language, a library function fprintf, also known as the format print function, sends output formatted to a stream. Even though it prints the message, it is impossible on the stdout console. It is almost similar to the normal printf() function except that it writes data into the file. Moreover, an additional argument is also present in the fprintf() part. It is a file pointer that points to the file where the formatted output will be written. The number of characters that write to the file will be returned if it succeeds. If the operation fails, an EOF (End-of-File) value will be returned.
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 is the pointer to a file object that finds the stream.
- The format, represented as a C string, contains the text to be written to the stream. It may contain embedded format tags that can be adjusted and formatted accordingly based on the values of subsequent arguments. 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. If the operation fails, it will return an EOF (End-of-File) value.
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 exponentially.
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 on 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 does 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. Next, the program will prompt the user to enter a user name The file mentioned above will include a record of this user name. When the code is executed, it becomes evident that a user name is required. Upon opening the file, both the name and index will be visible, as shown in the image below.
If n is changed to a value of 3, the program will prompt the user name three times, as shown in the figure below.
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 completed, enter the code for inputting the student roll number, name, and mark. Our file pointer contains information about the produced file’s filename.
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
We hope that this EDUCBA information on “fprintf() in C” was beneficial to you. You can view EDUCBA’s recommended articles for more information.