Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
fputc()
3.
fgetc()
4.
Frequently Asked Questions
4.1.
What is the use of fgetc () functions?
4.2.
What is the use of fputc () functions?
4.3.
What is the difference between fgetc () and fputc () functions?
4.4.
How many parameter do fgetc () functions takes?
4.5.
What is fopen () function?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

C fputc() and fgetc()

Author Alok Pandey
1 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

fgetc()

fgetc() is the polar opposite of fputc(). It takes the character from the file and displays it. Only one character is read at a time. To get the following character, we can print it as many times as we like. Because we must supply the file pointer as a parameter, the syntax is simple. To display the character on the output screen, we can save it in another character.

Syntax:  

int fgetc(FILE *pointer)

 

Let's take an example to demonstrate the fgetc() function in the following program.

Example Code

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int ch;
    FILE *fp;
    fp = fopen("data.txt", "r");
    if(fp == NULL)
    {
        printf("Error opening file\n");
        exit(1);
    }
    printf("Reading contents of data.txt: \n\n");
    while( (ch=fgetc(fp)) != EOF )
    {
        printf("%c", ch, ch);
    }
    fclose(fp);
    return 0;
}

 

Output

This text will be saved to the data.txt file

 

Explanation:

  • Variable ch of type int is declared.
  • A structure pointer variable fp of type struct FILE is declared.
  • fopen() → called with two arguments namely "myfile.txt" and "r".


You can also read about the dynamic arrays in c and Short int in C Programming

Must Read what is storage class in c

Frequently Asked Questions

What is the use of fgetc () functions?

The character is read from the file using the method fgetc(). If successful, it returns the character referenced by the file pointer; otherwise, it returns EOF.

What is the use of fputc () functions?

fputc() is used to write characters to the file. The letter C in the function's name stands for character.

What is the difference between fgetc () and fputc () functions?

fputc() is used to write characters to the file whereas fgetc() is the polar opposite of fputc(). It takes the character from the file and displays it. Only one character is read at a time. 

How many parameter do fgetc () functions takes?

fgetc() function takes one parameter ie a structure pointer variable fp of type struct FILE.

What is fopen () function?

The fopen() function creates a stream and opens the file supplied by filename. The mode variable is a character string that specifies the type of access to the file that has been requested.

Conclusion

In this article, we have extensively discussed the fputc() and fgetc() functions and their uses and implementation. 

We hope that this blog has helped you enhance your knowledge regarding the fputc() and fgetc() function and if you would like to learn more, check out our articles on  

 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. 

Enroll in our courses and refer to the mock test and problems available.

Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Previous article
fprintf() and fscanf() in C
Next article
C fputs() and fgets()
Live masterclass