In this article File Handling in C we give the information about File is a container which is used to store data. File Handling is a process in which data is stored in a file using a program.

What is file?

A file is a collection of bytes stored on a secondary storage device such as a disk. There are two types of files in a system -:

Text file

Binary file

Text File -:

Text files are normal .txt files. Creating these text files is very easy. You can easily create these text files using any simple text editor like Notepad

When you open these text files, you will see all the content of the file as plain text. If you want, you can easily edit or delete these contents.

Binary Files -:

There must be some file named .bin in your computer. This .bin file is called a binary file only.

Binary files store information in the form of binary numbers (0 and 1) instead of storing information in plain text.

In these binary files, we can store large amount of data. Since the information in these binary files is in binary form (in the form of 0 and 1), they cannot be read easily. These are not redable.

So friends till now we have come to know what are files and how many types of files are there.

Let us now know what is file handling and why is file handling needed?

Why use File Handling?

When the program closes the programmer, then all the data of the program gets destroyed. To store data in the program, the programmer uses some variables, arrays, structures, unions, but this data is not stored permanently. File Handling is used only to store it permanently. The files created during file handling, whether of different types (.txt, .doc etc.), are portable. It is also used in other computers.

File Handling in C

In C language, File is a container which is used to store data. File Handling is a process in which data is stored in a file using a program.

In C, we can perform many operations in a file using file handling such as creating, updating, reading, and deleting the file etc.

The following operations can be performed in a file:-

  1. Creating a new file.
  2. Opening the file.
  3. Reading the file.
  4. Writing the file.
  5. Deleting the file.
  6. Closing the file.

Functions for File Handling –

There are many functions in C library by which we can read, write, create and search and open a file etc. Below you have been given its complete list: –

Sr. No.

Function

Description

1

fopen ()

The work of this function is to open the file.

2

fprintf ()

By this function we can write the data in the file.

3 fscanf ()

By this the data is read from the file.

4 fputc ()

A character is written in the file using this function.

5

fgetc()

It reads one character from the file.

6 fclose()

The file is closed by this function.

7

fseek()

sets the file pointer at the given position.

8 fputw()

It writes integer to the file.

9

fgetw ()

It reads integer from the file.

10

ftell()

It returns the current position.

11 rewind ()

It sets the file pointer at the beginning of the file.

fopen() – File Handling

File is created and opened with fopen() function.

Syntax for fopen()

file_pointer = fopen(“file_name”, “mode_of_file”);

  • file_pointer: This is a file pointer whose data type is ‘FILE’.
  • file_name: The name of the file should be given along with its extension.

for e.g. file.txt, file.docx, .txt and .docx are the extensions of these files.

  • mode_of_file: The mode of the file decides in which mode to open it. There are three basic modes to open the file.

Source Code:

#include <stdio.h>

int main(){

FILE *fptr;

fptr = fopen(“sample.txt”,”w”);

return 0;

}

File Handling Modes:

Mode

Description

r

This opens the text file in read mode.

w

This opens the text file in write mode.

a

This opens the text file in append mode.

r+

This opens the text file in both read and write modes.

w+

This opens the text file in both read and write modes.

a+

This opens the text file in both read and write modes.

rb

It opens the binary file in read mode.

wb

It opens the binary file in write mode.

ab

This opens the binary file in append mode.

rb+

It opens the binary file in both read and write modes.

wb+

It opens the binary file in both read and write modes.

ab+

It opens the binary file in both read and write modes.

fclose() – File Handling

The file opened with fclose() function is closed.

Syntax for fclose()

fclose(file_pointer);

  • file_pointer: Pointer to the file which is to be closed.

Source Code:

#include <stdio.h>

int main(){

FILE *fptr;

fptr = fopen(“sample.txt”,”w”);

fclose(fptr);

return 0;

}

Reading data from file –

fscanf(), fgets() and fgetc() functions are used to read data from the file.

Syntax:

int fscanf(FILE *stream, const char *charPointer[])

Writing the file –

In C language, fprintf(), fputs(), fputc() functions are used to write the file.

Syntax:

int fprintf(FILE *stream, char *string[])

File handling program –

Advantage of File Handling

Its benefits are as follows:-

  1. Through this we can store the data generated from the program in a file. And this data can be used later. This increases reusability.
  2. By using files we can easily store large amounts of data.
  3. We can access any part of the file very quickly by using the functions present in the c library. This saves time.
  4. We can easily transfer files from one computer to another using file handling.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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