Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » C Language » File Handling in C with examples | File Operations in C

  • World Red Cross Day and Red Crescent Day | विश्व रेडक्रॉस दिवस
    World Red Cross Day and Red Crescent Day | विश्व रेडक्रॉस दिवस General Knowledge
  • MS Dhoni (Mahendra singh Dhoni) Cricket Biography in Hindi
    MS Dhoni (Mahendra singh Dhoni) Cricket Biography in Hindi Biography
  • Fundamental Duties of Indian Citizens
    Fundamental Duties of Indian Citizens | 11 मौलिक कर्तव्य हिंदी में General Knowledge
  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
    Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth Biography
  • Unicef day
    Unicef day is celebrated on December 11 | Speech on unicef day General Knowledge
  • Human rights day
    Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे General Knowledge
  • Real life Inspirational Stories in Hindi | Success story in Hindi
    Real life Inspirational Stories in Hindi | Success story in Hindi Biography
  • Indian Navy Day
    Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है? General Knowledge

File Handling in C with examples | File Operations in C

Posted on May 7, 2022May 7, 2022 By GeekCer Education No Comments on File Handling in C with examples | File Operations in C
File Handling in C with examples | File Operations in C

File handling in C language is the process of storing, updating, retrieving, deleting data from a file using a C program.

A file, as we all know, is a container for information. The files are kept on a secondary storage device. The file can keep the information forever if the secondary storage device is protected.

File handling in C allows us to use C program for creating, updating, reading, and deleting files. On a file, you can perform the following operations.

  • New file creation
  • Open existing file
  • Read from file
  • Write to file
  • Delete file

Table of Contents

  • Types of files to process in C program
  • File handling Functions in c with examples
  • Opening a File in the Program – File: fopen()
  • How to create a file in C?
  • File opening modes in C
  • Program to open a file in read mode
  • Write a program to display the contents of a file on the screen
  • C program to open a file in write mode
  • C program to open a file in append mode

Types of files to process in C program

  1. Text files
  2. Binary Files

What is difference between text and binary files? Using a basic text editor, we create a text file with the .txt extension. Binary files hold data and information in a binary format of 0s and 1s. The application creates binary files with the.bin extension, which substitutes for the lack of text files in the program.

File handling Functions in c with examples

To open, read, write more data, create a new file, close or delete a file, search for a file, and so on, the C language offers us with a number of functions.

#FunctionsDescription
1fopen()Open a file, either an existing one or a new one.
2fprintf() Adding data to a file that is already open.
3fscanf()Reads data from the file.
4fputc()Writes any character into the file.
6fgetc()Reads any character from the file.
7fclose()Closing a opened file.
8fseek()This function is used to move the file pointer to the desired location.
9fputw()Writes an integer value to file.
10fgetw()Reads an integer value to file.
11ftell()Checks the file’s current status.
12rewind()Sets a desired file pointer to the start of the file.

Opening a File in the Program – File: fopen()

To read, write, or update the file, we must first open it. The fopen() method is used to open a file in a C program.

FILE *fopen( const char * <file-name>, const char * mode );  

fopen("E:\\geekcer\\tfile.txt","w");
fopen("E:\\geekcer\\bfile.bin","rb");

How to create a file in C?

To create a file in C you need to specify the file name and opening mode in fopen() function. The file is created and then opened if the file does not exist. f a file already available on the system, this fopen() method opens file directly.

FILE *file;
file = fopen("<file-name>", "<opening-mode>");

File opening modes in C

Let’s have a look at some additional C program opening modes:

#ModelDescription
1rOpens a text file for reading only.
2wOpens a text file for writing.
3r+Allows you to read and write to a text file.
4w+Allows you to read and write to a text file.
5aOpening a file to append the content.
6a+Opening a file to append and reading data.
7rbOpening a binary file in read mode.
8rb+Opening a binary file in read and write mode.
9wbOpening a binary file in write mode.
10wb+Opening a binary file in reading and writing mode.
11abOpening a binary file in append mode.
12ab+Opening a binary file in reading and append mode.

Program to open a file in read mode

FILE * fp;
fp = fopen("<file-name>", "r");

#include 
int main() {
  FILE * fp;
  fp = fopen("file.txt", "r");
  if (fp == NULL) {
    printf("File is not available, please check!\n");
  }
  fclose(fp);
  return 0;
}

Write a program to display the contents of a file on the screen


#include 
int main() {
  FILE * fp;
  int c;
  fp = fopen("file.txt", "r");
  c = getc(fp);
  while (c != EOF) {
    putchar(c);
    c = getc(fp);
  }
  fclose(fp);
  return 0;
}

Output:
Hello, Welcome to C Programming Language.

C program to open a file in write mode

FILE * fp;
fp = fopen("<file-name>", "w");

#include 
int main() {
  FILE * file;
  /* Opening the file in write mode */
  file = fopen("file.txt", "w");
  /* Write string to opened file */
  fprintf(file, "%s", "Example of writing content into file.");
  /* Closing the file */
  fclose(file);
  return 0;
}

C program to open a file in append mode

FILE * fp;
fp = fopen("<file-name>", "a");

#include 
int main() {
  FILE * fp;
  fp = fopen("file.txt", "a");
  fprintf(fp, "%s", "The appended content.");
  fclose(fp);
  return 0;
}

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

C Language Tags:C program to read and write to a file, File in c programming examples, What are file modes in c?, What is file handling?

Post navigation

Previous Post: Storage classes in C with examples (auto, extern, register, static)
Next Post: Linked List in Data Structure and Algorithm, Types, Operations

More Related Articles

Programming with C Language Tutorial Programming with C Language Tutorial C Language
Top 100 C programs for geeks Top 100 C programs for geeks C Language
Statements in C Language | Types of statements in C Language Statements in C Language | Types of statements in C Language C Language
Linked List in Data Structure and Algorithm, Types, Operations Linked List in Data Structure and Algorithm, Types, Operations C Language
Storage classes in C with examples (auto, extern, static, register) Storage classes in C with examples (auto, extern, register, static) C Language
String Array in C | What is an array of string | String Functions String Array in C | What is an array of string | String Functions C Language

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • C Home
  • Constants and Operators
  • Statements in C Language
  • Array in C
  • String Array in C
  • Functions in C
  • Pointers in C
  • Structure in C
  • Storage Classes
  • File Handling in C
  • Top C Programs
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved