
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Explain putc() and getc() functions of files in C language
File is collection of records or is a place on hard disk, where data is stored permanently.
Operations on files
The operations on files in C programming language are as follows −
- Naming the file
- Opening the file
- Reading from the file
- Writing into the file
- Closing the file
syntax
The syntax for opening a file is as follows −
FILE *File pointer;
For example, FILE * fptr;
The syntax for naming a file is as follows −
File pointer = fopen ("File name", "mode");
For example,
fptr = fopen ("sample.txt", "r"); FILE *fp; fp = fopen ("sample.txt", "w");
putc( ) and getc( ) functions
putc( ) function is used for writing a character into a file.
The syntax for putc() function is as follows −
putc (char ch, FILE *fp);
For example,
FILE *fp; char ch; putc(ch, fp);
getc( ) function is used to read a character from file.
The syntax for getc() function is as follows −
char getc (FILE *fp);
For example,
FILE *fp; char ch; ch = getc(fp);
Example
Following is the C program for using 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 Question and answers in C Programming Language ^Z text on the file: Hi Welcome to TutorialsPoint Here I am Presenting Question and answers in C Programming Language
- Related Articles
- Explain fgetc() and fputc() functions in C language
- Explain the functions putw() and getw() in C language
- Explain unformatted input and output functions in C language
- Explain write mode operation of files in C language
- Explain read mode operation of files in C language
- Explain append mode operation of files in C language
- Explain important functions in math.h library functions using C language
- Explain the Random accessing files in C language
- Explain the custom header files in C language
- Explain the functions fread() and fwrite() used in files in C
- Explain scope rules related to the functions in C language
- What are the text files and binary files in C language?
- Differences between Difference between getc(), getchar(), getch() and getche() functions
- EOF, getc() and feof() in C
- Return type of getchar(), fgetc() and getc() in C

Advertisements