What is File Handling in C Programming?

Hey folks, welcome to this module we are going to talk about What is file handling in C Programming, how it is used, how to handle various operations like reading input and displaying output, and many more. So, let’s dive into more depth into this module.

What is File Handling in C Programming?

When you want to store data in files and work with files, then for that purpose we have file handling. When the program terminates, we lost all the data that were used in the program, so in order to avoid this loss, we can create files and store them accordingly.

What is file handling in C

Why files are needed in C Programming?

Let’s see some key points, that are why we need files in C Programming.

  • Storing all the data in your file will save all your data so that it can be used in the future as per requirement.
  • Now, if you have to work with a large amount of data, i.e., large data, then it will consume a lot of time to store them all, and if all that data are in files, it becomes a lot easier to work with all that data by using only a few commands.
  • Now, if you want to transfer these data from one place to another you can easily do it with files without losing any data.

What are the operations performed with files in C Programming?

There are four major operations that can be performed with files in C Programming, irrespective of the types of files.

  • Opening a file or Creating a file in C Programming
  • Reading a text in the file in C Programming
  • Writing content to a file in C Programming
  • Closing a file in C Programming

How many types of files are there?

There are mainly two types of files in C Programming, in which we can work while working with file handling.

Text files

  • These can easily be created by using any simple text editor like notepad and are normally available with the “.txt” extension.
  • The content in these files is available as plain text. The contents in these files are easily edited, modified, or deleted.
  • These files are easily readable.

Binary files

  • It is available in the “.bin” extension in your system.
  • The content stored in these files is in binary form i.e., 0’s and 1’s, they are not stored in plain text.
  • These files are not easily readable and can hold a high amount of data. It also provides more and better security compared to text files.

How to work with files in C Programming?

Now, that we know what are the basic operations that we can perform while working with files, so in order to execute all that operations what are the necessary things to declare in the C program?

In order to establish communication with the file, we need to declare one pointer of type file that can be achieved by:
FILE *fptr; // here fptr is the file pointer of type file.

How to open a file in C Programming?

Since we have to work with files, the very first step is to open a file that is available in the system or to create a new file as per requirement.

fopen( ) function is used to open a file in C, which is defined under the stdio.h header file. The syntax for opening a file is:

file = fopen( “file_open”, “mode” );
Here, file_open is the file to be opened and the mode tells for which purpose the file is to use, i.e., for reading, for writing, etc.

Some examples are:

// the file is the text file and the mode is to write in the file
file = fopen("old_file.txt", "w");
// the file is the binary file and the mode is to read the content
file = fopen("new_file.bin", "r"); 

How to close a file in C programming?

Once the file is opened, it should be closed also in order to get better execution. After performing a certain action or operation from a file, it should be closed at last. It can be achieved by fclose( ) function.

What are the modes in files in C Programming?

Modes tell the program that the particular file is open for which purpose, i.e., whether we have to write the content in the file, only we have to read the content from the file, or we have to append some content, etc.

So, let’s see the different types of modes available in C Programming.

Mode Meaning
r For reading the text file
rb For reading the binary file
w For writing in a text file
wb For writing in a binary file
a For appending the content in a text file
ab For appending the content in a binary file
w+ Open for reading and writing both and if the file does not exist it creates a new file, and if the file exists it overrides the content of the file
wb+ Open for both reading and writing to operate in binary mode.
r+ Open for both reading and writing, and if the file does not exist it return null.
rb+ Open for both reading and writing in binary mode.
a+ Open for both reading and appending the content of files.
ab+ Open for both reading and appending the content of files in binary mode.

These are some of the different modes available in C, which are used for different purposes for working with files.

Program to write to a text file in C Programming

In this section, we will see a program that will open a text file and write some content in it.

#include <stdio.h>
#include <stdlib.h>
int main(){   
int number;   
FILE *fptr;   
// the path of the file should be correct and the mode also
fptr = fopen("C:\\use_my_notes.txt","w");
// if file not opened correctly
if(fptr == NULL)     
 {      
   printf("Error in opening a file!");       
}   
printf("Enter some number: ");   
scanf("%d",&number);   
// for writing a content to a file
fprintf(fptr,"%d",number);      
// closing the opened file.
fclose(fptr);      
return 0;      
}

Here, in the above program, we have opened a file use_my_notes.txt in a writing mode, so that we can write some content in this file, and we are doing this with the help of fprintf( ) function and at last, we have closed the that file.

Program to read from a text file in C Programming

In this section, we will see a program that will open a text file and read the content from a file.

#include <stdio.h>
#include <stdlib.h>
int main(){   
int number;   
FILE *fptr;   
// the path of the file should be correct and the mode also
fptr = fopen("C:\\use_my_notes.txt","r"); 
// if file not opened correctly
if(fptr == NULL)     
 {      
   printf("Error in opening file !");       
}   
// reading the content from a file
fscanf(fptr, "%d", &number);    
printf(“Value of number = %d",number); 
// closing a file
fclose(fptr); 
return 0;
}

Here, in the above program, we have opened a file use_my_notes.txt in a reading mode, so that we can read the content from this file, and we are doing this with the help of fscanf( ) function at last, we have closed that file.

I hope the What is File Handling in C Programming module helped you all a lot in gaining full knowledge about file handling. All the basic four operations, etc. Stay connected for such meaningful modules. Until then, keep learning, Happy coding!