File handing in C | File Operation in C | File Handling Functions in C

In this article File handing in C  we give the information about file operation in c, file type in c, how to create a file in c and file handling functions in c.

File handing in C:

What is File:-

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

  • Text file
  • Binary file

1. 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 contents of the file as plain text. If you want, you can easily edit or delete this content.

2. Binary Files -:

You must have some file named .bin in your computer. This .bin file is called a binary file. Instead of storing information in plain text, binary files store information in the form of binary numbers (0 and 1).We can store large amount of data in these binary files. Since the information in these binary files is in binary form (in the form of 0’s and 1’s), they cannot be read easily. These are not readable.

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 by using file handling such as – creating, updating, reading, and deleting the file etc.

Operations in a file Handling:-

1. Creating a new file.

2. Opening the file.

3. Reading the file.

4. To write to the file.

5. Delete the file.

6. Closing the file.

File handing in C:

Functions for File Handling –

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

No. Function Description
1 fopen() This function is used to create a new file or open an existing file.
2 fprintf() Through this function, we can write the data in the file.
3 fscanf() reads the data from the file.
4 fputc() Using this function a character is written in the file.
5 fgetc() It reads one character from the file.
6 fclose() This function is used to close the file.
7 fseek() It sets the file pointer at the given position.
8 fputw() It writes the integer to the file.
9 fgetw() It reads the integer from the file.
10 ftell() It returns the current position.
11 rewind() It sets the file pointer at the beginning of the file.

 Opening or Creation of a new file:-

  1. The fopen() function is used to create files or open created files in C language.
  2. its function is defined in a header file named stdio.h Before reading, writing, or updating a file, we have to open the file.
  3. The fopen() function is used to open the file.

Its syntax is given below:-

Syntax -:

FILE *fp;

fp = fopen(“file_name”,”mode”);

  • Here fp is a FILE pointer which will point to the file to be opened by fopen().
  • FILE is a structure that contains information related to the file such as name, size, buffer size, current position, end of file etc.
  • file_name – Here the name of the file that we want to open will come.
  • Mode – We decide the mode according to which operation (like read, write, update) we want to do in the file.

Example:-

FILE *fp;

fp = fopen(“fileName.txt”, “w”)

Here in this example fileName.txt is the name of the file we want to open and w is the file opening mode which indicates that we are opening this file to perform the write operation. 

Mode

Description

r

It 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+

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

w+

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

a+

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

rb

This opens the binary file in read mode.

wb

This opens the binary file in write mode.

 Closing the file:-

After doing any work in the file, we should always close the file, fclose() function is used to close the file.

Its syntax is as follows:-

fclose(fptr);

Here fptr is a file pointer which is associated with the file to be closed.

Reading data from file –

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

Its syntax is given below:-

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

To write a file –

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

Its syntax is given below.

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

File Handling Example:-

#include<stdio.h>
#include<stdlib.h>
int main()
{
char ch;
FILE *fp;
fp = fopen(“C:\\myprogram.txt”,”w”);
if(fp == NULL)
{
printf(“Error”);
exit(1);
}
printf(“Enter any character: “);
scanf(“%c”,&ch);
fprintf(fp,”%c”,ch);
fclose(fp);
return 0;
}

Advantage of File Handling:-

Its benefits are as follows:-

1. Through this we can store the data generated from the program in the file. And this data can be used later. This increases reusability.

2. Using files, we can also store large amount of data easily.

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 computer using file handling.

Related Link:

Some More: DBMS/ WT/ DMDW

Santosh Nalawade

Work as Assistant Professor and Web Developer.

2 thoughts on “File handing in C | File Operation in C | File Handling Functions in C

Leave a Reply

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