The Wayback Machine - https://web.archive.org/web/20240912195927/https://www.geeksforgeeks.org/fgetc-fputc-c/
Open In App

fgetc() and fputc() in C

Last Updated : 06 Jul, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

fgetc()

fgetc() is used to obtain input from a file single character at a time. This function returns the ASCII code of the character read by the function. It returns the character present at position indicated by file pointer. After reading the character, the file pointer is advanced to next character. If pointer is at end of file or if an error occurs EOF file is returned by this function. 
Syntax: 
 

int fgetc(FILE *pointer)
pointer: pointer to a FILE object that identifies 
the stream on which the operation is to be performed.

 

C




// C program to illustrate fgetc() function
#include <stdio.h>
 
int main ()
{
    // open the file
    FILE *fp = fopen("test.txt","r");
 
    // Return if could not open file
    if (fp == NULL)
      return 0;
 
    do
    {
        // Taking input single character at a time
        char c = fgetc(fp);
 
        // Checking for end of file
        if (feof(fp))
            break ;
 
        printf("%c", c);
    }  while(1);
 
    fclose(fp);
    return(0);
}


Output: 
 

The entire content of file is printed character by
character till end of file. It reads newline character
as well.

 

Using fputc()

fputc() is used to write a single character at a time to a given file. It writes the given character at the position denoted by the file pointer and then advances the file pointer. 
This function returns the character that is written in case of successful write operation else in case of error EOF is returned. 
Syntax: 
 

int fputc(int char, FILE *pointer)
char:  character to be written. 
This is passed as its int promotion.
pointer: pointer to a FILE object that identifies the 
stream where the character is to be written.

 

C




// C program to illustrate fputc() function
#include<stdio.h>
int main()
{
    int i = 0;
    FILE *fp = fopen("output.txt","w");
 
    // Return if could not open file
    if (fp == NULL)
      return 0;
 
    char string[] = "good bye", received_string[20];
 
    for (i = 0; string[i]!='\0'; i++)
 
        // Input string into the file
        // single character at a time
        fputc(string[i], fp);
 
    fclose(fp);
    fp = fopen("output.txt","r");
 
    // Reading the string from file
    fgets(received_string,20,fp);
 
    printf("%s", received_string);
 
    fclose(fp);
    return 0;
}


Output: 
 

good bye

When fputc() is executed characters of string variable are written into the file one by one. When we read the line from the file we get the same string that we entered.

 



Similar Reads

Getting System and Process Information Using C Programming and Shell in Linux
Whenever you start a new process in Linux it creates a file in /proc/ folder with the same name as that of the process id of the process. In that folder, there is a file named "status" which has all the details of the process. We can get those Process Information Through shell as follows: cat /proc/1/status As can be seen, it displays most of the i
2 min read
How to Set Up and Use Apple Watch for Health and Fitness Tracking?
In today's technology, the Apple Watch is an effective tool to measure and track your fitness and health. It has several internal features that can help you keep track of your daily exercises, keep an eye on your overall activity levels, and even manage your general well-being to simplify the routine. It can provide important insights into your eve
4 min read
Worms, Viruses and beyond !!
This article introduces some very basic types of malicious content which may harm your PC in some way or the other.. The Threat The computer systems may become a victim of virus, worm, hacking etc types of attacks. The computer systems may crash, sensitive data can be stolen and misused or driver problems may arise in few cases Who may do it It is
5 min read
How to Install and Use Vim on DOSBox?
DOSBox is a program that emulates all the functionality of MS-DOS. The DOSBox is primarily used to run older video games and includes simulation for sound, graphics, input, and networking. It is free to use and can also be used as an alternative of MS-DOS for some use cases. In this article, we will look into how you can use DOSBox with all the fun
4 min read
App Development for Android in 2017: Challenges and Solutions
Since Android OS took over the world, it’s hard to imagine the outsourcing niche without a mobile app development. Usually, the launching of the app is a quite time-consuming process. When a company decides to make an enterprise application, it is needed here and now. That’s why the mechanism of building hybrid or native apps for Android usually re
10 min read
Introduction to Xamarin | A Software for Mobile App Development and App Creation
The entire world is now surrounded by billions and trillions of mobile Tech which is inevitable. The major share of the development of mobile apps is taken by the Google's Android, Apple's iOS, and Microsoft's Windows. Every new learner or newbie in Mobile Development Domain finds himself in the dilemma of choosing the platform to start with. They
9 min read
Futuristic Solution to Privacy and Security in online social media
Social media has become an important part of our lives. This has led to rising of lots of messaging apps like Messenger, WhatsApp and so on. These are great for communication and have a lot of features for people to communicate effectively. On one side there is an uprising advent of technology in people communication, on the other side, it also has
4 min read
Different Python IDEs and Code Editors
What Are IDEs and Code Editors : IDE is a combination of tools that help in software development. IDE makes coding easier. As the name implies the Integrated Development Environment(IDE), it is a pack of tools combined or integrated together to establish software development at ease. The combination of tools include, a code editor that helps with l
5 min read
How to Detect and Remove Keylogger From Android?
Android operating system is the largest installed base among various mobile platforms across the globe. Hundreds of millions of mobile devices are powered by Android in more than 190 countries of the world. It conquered around 71% of the global market share by the end of 2021, and this trend is growing bigger every other day. As Android is the most
5 min read
Resume Building - Resources and Tips
Most of the candidates - be they freshers, or experienced have this question: why they are not getting calls from good companies? For freshers, it's difficult to mention their key skillset or get a job interview call due to not having any experience in the industry. A lot of candidates are unaware of whether their resumes match the job description
7 min read
Some Useful Tips for Choosing Best VPN and Recommended VPN Services
A VPN (Virtual Private Network) is a network that extends over many public networks connecting users "virtually" to a single network. This is done by utilizing a VPN client on the user's machine and VPN server(s). This simple application helps a user in many different ways: bypassing access restrictions, protecting sensitive data from eavesdropping
5 min read
Career Paths For Software Developers and Programmers in 2019
There are so many beginners and experienced programmers who actually have no idea about enormous career opportunities available in software development or as a programmer. Every day a new technology is coming or something new is introducing to make our life much better and easier. Many programmers and developers work hard in their own role to intro
10 min read
How to create a Folder without any name and icon in Windows OS?
Have you thought about hiding your confidential content or data somewhere that no one else could find out? What if this article tells you that you will be able to do just that, but the data will not be in plain and everyday sights of anyone, but still no one will be able to find it. Have you ever tried not to leave it blank? You must have tried and
2 min read
Why are there two folders - Program Files and Program Files (x86) in 64-bit Windows OS?
Have you ever wondered why you have two different Program Files (Program Files and Program Files (x86)) on your 64-bit Windows Operating System? Well, you will get the answer to this question by going through this article. Earlier on, the Windows OS has 32-bit version only. If you are still using a 32-bit of Windows today, you will notice that you
3 min read
7 Tips and Tricks to Learn Programming Faster
One of the common and basic questions among students or someone who wants to learn code is "How can I learn to code fast and make a career as a programmer?". Whether you are a student, fresher or experienced person trying to switch your job in programming you will definitely try to find tips and tricks to learn to program quickly and effectively. T
7 min read
How to Delete and Recover Firebase Project?
Here, we are going to Delete and Recover Firebase Project. This feature will be useful when we have by mistake deleted our project and we want to recover that project. Or it may be possible that we had deleted our project because we had no use of that, but now we need that firebase project. Note: A deleted firebase project can be recovered within o
1 min read
Unvalidated Redirects and Forwards
Unvalidated Redirects and Forward Vulnerability, also sometimes referred to as URL Redirection Vulnerability, is a type of bug found in the Web Application. In this type of vulnerability, the attacker uses to manipulate the URL and sends it to the victim. As soon as the victim opens the URL, the website redirects it to a malicious website or websit
3 min read
How I Found a Bug In Facebook and Earned 500 Dollars?
Facebook has a bug bounty program that allows you to find vulnerabilities and report security issues ethically. If the security issue that you found is harmful to the privacy of Facebook's users or its servers then they will pay money as a reward for your efforts. So I also found a bug on Facebook and earned $500. Let's discuss the bug. The bug was
2 min read
Data Visualization Using Chartjs and Django
Prerequisite : django installation With the growth of data, data visualization in become a import part here we will implement chart for our data in our web apps using chartjs with django. Django is a high-level Python Web framework based web framework and chartjs is an easy way to include animated, interactive graphs. Modules required : django : in
4 min read
How to Enable Copy and Paste in Oracle VirtualBox?
In this article, we will see how to enable copy and paste from a Windows host to a VirtualBox and vice-versa. We've taken the VirtualBox guest additions which gives us various additional tooling, specifically giving us the ability to copy and paste. It also enables bi-directional copy and pastes. Step 1: Start the Virtual Box and go to Settings as
1 min read
Types of Monitor Panels and Which One is Better?
When it comes to monitors they come in different shapes, sizes, and panels. So, what are these panels, and how to select a better one for your purpose?... Currently, the best available panels in the market are IPS (In-Plane Switching), TN (Twisted nematics), and VA(Vertical Alignment) panels. And in this article, we will discuss these three panels
5 min read
Exposing Localhost Server over Internet Using ngrok and VS Code
We setup and build many projects but before moving them to the production server, we always test the project, and we share the project among our team to share the progress, we share our project code on GitHub as it supports version control system and, but what if we do not want to share the code, but we only need to share the outcome to the client,
3 min read
What is Terminal, Console, Shell and Kernel?
As a fresher, while learning Linux we get confused with the actual meaning of the terms like-Terminal, Console, Shell, and Kernel. Sometimes we mix up their usage which is technically incorrect so having a clear grasp of what every word actually means and how and where they should be used is very important. 1. Terminal:For those having Linux OS, wh
4 min read
How to Import and Export Data to Database in MySQL Workbench?
Utilizing MySQL Workbench, a unified visual database designing or graphical user interface tool is necessary when working with database architects, developers, and administrators. It was made and is updated by Oracle. It offers comprehensive administration tools for server configuration, user administration, backup, and many other tasks as well as
3 min read
How to Pause and Play a Loop in JavaScript using Event Listeners ?
Given below is a JavaScript program for DOM manipulation which is basically about How to Pause and Play a loop in JavaScript using event listeners (not to be confused with delay). In this article, we are using JavaScript with HTML so we need a web browser i.e., Chrome (recommended) or Electron Application. Pausing and playing a loop is such a diffi
4 min read
OWASP Top 10 Vulnerabilities And Preventions
OWASP basically stands for the Open Web Application Security Project, it is a non-profit global online community consisting of tens of thousands of members and hundreds of chapters that produces articles, documentation, tools, and technologies in the field of web application security. Every three to four years, OWASP revises and publishes its list
6 min read
Blue Screen of Death (BSOD) and Stop Error
In this article, We will cover what is blue screen error(BSOD) and how to troubleshoot and fix this annoying issue on Windows 10. Blue screen error : Blue screen error (Blue Screen Of Death) is basically a stop error or exception error screen that is displayed on Windows computers following a fatal system error. It indicates a critical event, in wh
3 min read
Difference Between AWS S3 and AWS EBS
The AWS S3(simple storage service ) and AWS EBS(elastic block storage) are two different types of storage services provided by Amazon Web Services. This article highlights some major differences between Amazon S3 and Amazon EBS. AWS Storage Options: Amazon S3: Amazon S3 is a simple storage service offered by Amazon and it is useful for hosting webs
6 min read
NetBeans Look And Feel Plugin Using Dracula of IntelliJ IDEA
NetBeans IDE is one of the most widely used IDE. It is a tool for a variety of technologies, in particular for technologies making use of Java, JavaScript, PHP, and C/C++. But its popularity is taking a hit with the introduction of new visually stunning IDEs like Sublime. But newer updates in the NetBeans has made it extremely configurable, includi
3 min read
Creating Java Project Without Maven in Apache NetBeans (11 and Higher)
In this article, you will find the steps to create a simple Java Project without Maven in Apache NetBeans (v 11 and higher). Since maven has launched, the creation of Java Project directly has been shifted under the "Java with Ant", due to which the geeks who upgrade themselves to the new Apache NetBeans face problem finding the option to create a
3 min read
Article Tags :