File Handling in C Programming: Definition, Functions, Operations, Need

By Anjnee Bhatnagar|Updated : September 19th, 2022

File handling in C refers to the method applied to use software to store the available data or information in a file. With the help of file handling in C, the C language stores all the data accessible in a program into a file. Reusing this data in any software can be obtained or extracted from these files as per requirement.

We can use file handling in C features to write, read, update, and remove files stored on the local file system. In this article, according to the GATE Syllabus for CSE, we shall examine File Handling in C and its functions, operations, and needs.

Table of Content

What is File Handling in C?

"File handling in C" helps us create, manipulate, update, and delete files stored on the local file system using C programs.

File Handling in C Definition

File Handling in C programming language is a feature that lets you operate the files with the help of C programs or C code.

Sometimes merely displaying the facts on the console is insufficient. The quantity of data displayed on the console may be extremely small or very vast. Because the memory is volatile, it is hard to repeatedly restore the programmatically generated data. However, we can store data on the local file system, which is volatile and accessible whenever we need to. Here, file handling in C becomes necessary.

What is a File in C?

A file in C can be defined as a sequence of bytes. A file can be a text file straight away or a binary code sequence. Regardless of this, it is treated as a sequence of bytes.

The C programming language for managing files on storage devices offers access to both low-level (OS level) calls and high-level functions.

Types of Files in C

A file is typically used in computers to store user data. In other words, a computer uses files to store data. These data files are now available in the C language in 2 different forms, namely:

  • Binary Files
  • Text Files

Now we shall briefly discuss each type of file in C.

Binary File

The binary files contain information and data using the 0s and 1s binary coding scheme (the binary number system). As a result, the files take up far less storage space. In other words, binary files retain data and information like a computer does in its memory. As a result, it is easier to access than a text file.

The disadvantage of text files in a program is overcome by the binary files, which are produced with the extension.bin and can only be read by computers, as opposed to humans, who cannot read them.

Text File

The most fundamental types of files that a user in a C program can produce are text files. We produce the text files using a basic text editor with the.txt extension. These files internally hold information in ASCII character format. However, the content or text appears in a readable format when we view them.

Thus, accessing and using text files is quite simple. There is, however, one significant drawback: lack of security. Information isn't secure in a text file because it can be retrieved quickly. Apart from the security issue, text files occupy a larger storage space than binary files.

Functions for File Handling in C

A file can be opened, read, expanded upon, created, closed, deleted, searched for, etc., via several different functions. In C, these are referred to as file handling operators.

The following is a list of the functions that let you do that:

Description of Function

Function in Use

 To open an existing file or a new file


For writing data into a file


For reading the data in a file


For writing any character into the program file


For reading the character from a file


To close the program file


To set the file pointer to the intended file position


For writing an integer into an available file


To read an integer from the given file


For reading the current position of a file


To set an intended file pointer to the file's beginning itself


Operations in File Handling in C

A user can update, create, open, read, write, and finally delete the file/content in the file that already exists on the local file system of the C program through the file handling process. The main file operations you can carry out in a C program are listed below:

  • Opening a file
  • Creating a new file
  • Writing data to file
  • Reading data from file
  • Deleting data from file

Need for File Handling in C

Occasionally, a program's output after it has been compiled and run does not meet our objectives. We might want to check the program's output several times in these circumstances. It takes a lot of work for any coder to repeatedly compile and run the same software. This is the precise situation when file handling is helpful. Some of the features of file handling in C that provide a user or programmer with convenience are:

  • Portability of the source code.
  • Provides storage capacity.
  • Provides reusability of the programs that are stored in the file.
Important Topics for Gate Exam
Volumetric StrainWhat Are Elementary Signals
Whats A Transmission Line2's complement
Free Body DiagramHuffman Coding
Kruskal AlgorithmMechanical Properties of Engineering Materials
Moulding SandPolymers


write a comment

FAQs on File Handlin in C

  • File handling in C allows performing various functions over a file in the system. Using features of file handling, we can perform functions like opening an existing file, creating a new file, reading the contents of a file, and deleting the contents of the file. Example:

    To open a file: fopen() is used to close a file fclose() is used.

  • File Handling in C provides the user with various features and adds to the power of C programming. File handling in C provides reusability and portability of the data. It also provides storage capacity to the programs.

  • Data is permanently stored in a storage device using files. The ability to save program output in a file and execute various operations on it is made possible by file handling. A device's input and output actions are carried out on a stream, which is an abstraction of that device.

  • In C, there are two types of files, namely: Text files and Binary files. For text files, the extension used is ".txt," and for binary files, the extension used is ".bin".

  • When a program crashes, all of the data is lost. However, saving it in a file will keep your data safe even if the program crashes. It typically takes a long time to enter a lot of data in such a case.

Follow us for latest updates