Explain putc and getc Functions of Files in C Language



A file is a collection of data stored in secondary memory. Files are used to store information that programs can determine. A file represents a sequence of bytes, whether it is a text file or a binary file. It is a collection of records or a location on the hard disk where data is stored permanently.

Operations on Files

The operations on files in the C programming language are as follows ?

  • Naming the file
  • Opening the file
  • Reading from the file
  • Writing into the file
  • Closing the file

Let us look at some of these operations.

Opening the File

To write something into file, we need to open it using the fopen() method. The syntax for opening a file is as follows ?

FILE *File pointer;

To name/rename a file you need to pass the desired name as a parameter to the Open() method. The syntax for naming a file is as follows ?

File pointer = fopen ("File name", "mode");

For example, if the desired name of a file is "sample.txt" the Python code to write into it would be as follows −

fptr = fopen ("sample.txt", "r");
FILE *fp;
fp = fopen ("sample.txt", "w");

The putc() Function

The putc() function is used to write a character to a file. It converts the character to another char and writes it to the current position in the output stream. The syntax for putc() function is as follows ?

putc (char ch, FILE *fp);

For example,

FILE *fp;
char ch;
putc(ch, fp);

The getc() Function

The getc() function is used to read a character from a file and returns it as an integer. This function reads the next character from the given file and moves the file's position forward. It is difficult to read one character at a time. The syntax for getc() function is as follows ?

char getc (FILE *fp);

Example: File Character Operations

This code writes user input to a file, then reads and displays the file's content character by character using file pointers and standard input functions. Below is the C program demonstrating the use of putc() and getc() functions ?

#include<stdio.h>
int main(){
   char ch;
   FILE *fp;
   
   fp=fopen("std1.txt","w"); //opening file in write mode
   
   printf("enter the text.press cntrl Z:");
   while((ch = getchar())!=EOF){
      putc(ch,fp); // writing each character into the file
   }
   fclose(fp);
   
   fp=fopen("std1.txt","r");
   printf("text on the file:");
   while ((ch=getc(fp))!=EOF){ // reading each character from file
      putchar(ch); // displaying each character on to the screen
   }
   fclose(fp);
   
   return 0;
}

Output

When the above program is executed, it produces the following result ?

enter the text.press cntrl Z:
Hi Welcome to TutorialsPoint
Here I am Presenting Questions and answers in C Programming Language
^Z
text on the file:
Hi Welcome to TutorialsPoint
Here I am Presenting Questions and answers in C Programming Language

Example: Reading From a File

The C program reads and displays characters from "test.txt" until the end of the file, then checks for errors and closes it.

#include <stdio.h>
int main() {
   char c;
   printf("Enter a character:");
   c = getc(stdin);
   printf("
Character is: %c", c); return 0; }

Output

The result is obtained as follows ?

Enter a character: Tutorialspoint
Character is: T
Updated on: 2024-12-06T14:45:51+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements