If you are searching for fprintf in c you are at the right blog post. Each and every piece of information about fprintf in c is explained in this blog post.
What is fprintf in c programming?
fprintf function in c is used to format the output to a file instead of stdout console.
It is used to write a set of characters to a file. Instead of printing some characters to the console, we can make use of fprintf to write the output to the file.
The character ‘f’ in fprintf stands for formatted. The data formatted in such a way that the user can understand it or read it in an easy way.
It returns the number of characters written.if any error occurs it returns negative value(-1).
fprintf in c found in standard input output library that is stdio.h
.
prototype
int fprintf(FILE *file_pointer, const char *format_string,.......);
Parameters for fprintf
Here FILE*
is a file pointer which points to a file where output is to be written. Format string describes the output format. Variables are specified like in printf
to use their values.
Return value of fprintf
fprintf returns an int value i.e. the number of characters written on to a file. If it fails to execute then returns -1.
Example program for fprintf in c
#include <stdio.h>
int main()
{
FILE *fpointer;
fpointer=fopen("abc.txt","w");
fprintf(fpointer,"Scholarsoul.com\n");
fclose(fpointer);
return 0;
}
Code explanation
fprintf is called by passing the text value to be printed and a file pointer. This file pointer points to the file which is being opened for a specific task.
Differences between fprintf and fgetc in c
fgetc
function in c is used to read a character from a file.fprintf
function puts the character to a file.
Both functions take one character at a time.
fprintf
is an output function where as fgetc is an input function.
Both functions take pointer to advance.
As fgetc
is input function it takes only one argument whereas fprintf
is an output function so it takes two arguments. As we can see in their prototypes,
int fgetc(FILE *strm);
int fprintf(FILE *strm,const char *formt,...);
fgetc
the function returns the character read as unsigned char otherwise error. Whereas fprintf the function returns a total number of characters written into the file otherwise negative number as an error.
Look at the below table
fprintf in c | fgetc in c |
It sends formatted data to a specified file. | Used to read character from a file |
It takes one character at a time | It also takes one character at a time. |
Output function | Input function |
Uses pointer | Uses pointer |
Takes two or more arguments. | Takes only one argument |
int fprintf(FILE *strm,const char *formt,...); | int fgetc(FILE *strm); |
Differences between fprintf and fscanf in c
fscanf
function reads formatted data from a stream. fprintf
function puts formatted data into a file.
Both functions requires a pointer which points to a file.
fprintf
function is used as output function .fscanf
function is used as input function.
As both are file handling i/o functions,they have same prototype.
fscanf
function returns the input data which are matched or zero on matching failure. whereas fprintf
the function returns a total number of characters written or a negative number on error.
fscanf
functions remove all the whitespaces before reading the characters. Whereas fprintf
function does not remove any whitespace.
Look at the below table
fprintf in c | fscanf in c |
Writes formatted data to a file | Reads formatted data from stream |
Needs pointer | Needs pointer |
Output function | Input function |
int fprintf(FILE *strm,const char *formt,...); | int fscanf(FILE * fpointer, conast char *frmt[argmt,...]); |
Returns negative number on error | Returns zero on error |
Does not remove any whitespaces | Removes all whitespaces before seek. |
Differences between fprintf and putw in c
putw
function is used to write integer value into a file. whereas fprintf
function is used to write formatted data into a file.
Both functions needs file pointer.
Both are output functions.
Prototype is as below,
putw(int num,FILE *fpointer);
int fprintf(FILE *strm,const char *formt,...);
In putw
function pointer is used to write numbers into file. Whereas in fprintf
pointer is used for seek operation.
Look at the below table
fprintf in c | putw in c |
Used to write formatted data into a file | Used to write integer value to a file |
Needs pointer | Needs pointer |
Output function | Output function |
int fprintf(FILE *strm,const char *formt,...); | putw(int num,FILE *fpointer); |
Returns toatal number of characters otherwise negative number | Returns 0 after successful completion otherwise negative number |
People also ask for
What is the use of fprintf()
?
This function is used to write formatted data to a file. This function is supported in the standard input/output header.
What is the return value of fprintf()
?
It returns the number of bytes that are printed. otherwise, it returns the negative value as an error.
Look at the errors listed below:
Invalid file identifier.
EILSEQ
EINVAL
ENOMEM
EOVERFLOW
Is fprintf()
faster than printf()
?
Both functions are equivalent. printf is more convenient while printing to the console.
fprintf is easier to modify if one wants to change the output.
What is fprintf and fscanf in C?
fprintf
in c is an output function that writes output to a file. fscanf
in c is an input function that reads input to a file.
What does fprintf
stands for?
Print out to a particular FILE *fp according to the format specifier.
What is the difference between fprintf
and fwrite
in c?
fprintf
writes a string to a file whereas fwrite
writes bytes to a file.
How can I print ‘hello world’ in C without using printf and puts in the program?
You can use fprintf to write to stdout using stdout as a file pointer. Have a look at the program below.
How can I print ‘hello world’ in C without using printf and puts in the program?
//we have written hello scholar soul instead of hello world.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main(int argc, char const *argv[])
{
fprintf(stdout,"Hello Scholar soul!");
return 0;
}
Code explanation
We can print a statement in c without using printf and puts
functions as seen in above code. Here with the help of fprintf
function we are putting statement “Hello scholar soul!” into a file. Only difference here is we are using one pointer named stdout. stdout is a file descriptor associated with standard output console.