Introduction
The C programming language offers many inbuilt functions for handling files. But let’s say you want to store the output of a function or normal data for future uses. Then what should you do?
That’s where the file handling feature of the C language comes into the picture.
You can store the volatile output or data onto the local file system, and it can be accessed every time.
In this blog, we will discuss the puts() and fgets() functions of the c language.
Recommended Topics, Sum of Digits in C and C Static Function.
fputc()
fputc() is used to write characters to the file. The letter C in the function's name stands for character. In this function, two parameters are passed to the function as input. The first is a single character that we'd like to save in the file. The pointer to the file is the second parameter. It returns the character to the screen after successful implementation. It would display an EOF exception if it couldn't do so due to any other fault. The term EOF refers to the end of a file. When working with files, you'll come across a lot of these exceptions.
Syntax:
int fputc(int char, FILE *pointer)
Let's take an example to demonstrate the fputc() function to demonstrate the following program. you can practice by yourself with the help of Online compiler.
Example Code
#include <stdio.h>
main(){
FILE *fp;
fp = fopen("data.txt", "w");
fputc('This text will be saved to data.txt file',fp);
fclose(fp);
}
Output:
On opening the file data.txt
Note: This text will be saved to the data.txt file
Explanation:
In the main function
- The struct FILE structure pointer variable fp is declared.
- Function fopen() is called with two arguments namely "data.txt" and "w" .
- fputc() --> used to write characters to the file.
- fclose() --> used to close the file.
Click on the following link to read further: Features of C Language, and Tribonacci Series
Must Read Passing Arrays to Function in C