File Handling in C

Introduction to File Handling in C

File Handling is very useful while storing your data in the storage device permanently, File Handling in C is storing up of data in a file by the use of C programming. Sometimes when data is very large it cannot be displayed, due to some console limitation. In these type of condition File handling is used in C programming to help you deal with these problems.
File handling in C programming

Types of File in C programming

There are mainly 2 types of file that are there in C programming, they are as follows:

  • Text File
  • Binary File

Text File: These files are normal test file written in notepad and can be easy understood by a normal person who knows how to read. These files uses “.txt” extension to store in your memory. 

Binary File: These files are in the form of 0’s and 1’s. Text files are converted into Binary files so that it could be easily understood and accessed  by the computer. These files occupy lesser space in the memory and uses the extension of “.bin”. 

There are various functions that can be performed on File Handling in C. These functions are mentioned below:

  • Creation of a new File
  • Reading an existing File
  • Writing an existing File
  • Opening an existing File
  • Moving to a specific location in the File
  • Closing a File

Steps For using a File in your C program

  • Creating a new file pointer
  • Using fopen() function to open a File
  • Processing the file using various methods and functions.
  • Using fclose() to close the file.

Open a file- Creating or Editing a File using fopen()

Before performing any function in a file, it must be opened first for its editing to begin.

There are 2 steps before a file can be created or edited, they are:

  • Creating a file pointer of FILE type .
  • The below code will use the fopen() function to create a new file if the file name “nfile.text” does not exist and if its already exist then the file will open in write mode.
fstream nfile;
nfile.open(“nfile.txt”, ios::in);

There are various modes in which a file can be accessed , they are mentioned below in the table:

S.No Mode Description
1 r Used to open a text file in READ mode.
2 w Used to open a text file in WRITING mode.
3 a Used to open a text file in APPEND mode.
4 r+ Used to open text file in both READ and WRITE mode and this mode cannot create a new file if it doesn’t exist.
5 w+ Used to open text file in both READ and WRITE mode and this mode can create a new file if it doesn’t exist. Existing data will be overwritten by this.
6 a+ Used to open text file in both READ and WRITE mode and this mode can create a new file if it doesn’t exist. Existing data will not be overwritten.

Function used in File Handling in C programming

There are various pre-defined functions that you can use in C library. They all have unique functions and can be used according to use. Some of the functions are mentioned bellow: 

S.No Function Description
1 fopen() It is used to open a new file or edit an existing file.
2 fprintf() It is used to write a set of data into the file.
3 scanf() It is used to read strings from the file.
4 putc() It is used to write a particular single character as per the requirement into the file.
5 getc() It is used to return(read) a single character from a specific available file.
6 fseek() It is used in setting the file cursor to an intended given position.
7 fputw() It is used to insert a particular integer into an available file in write mode.
8 fgetw() It is used to read a particular integer value from a given file.
9 fclose() It is used to close the open program file.
10 rewind() It is used in setting the file cursor at the beginning of the intended file.

Example 1:

The below program is used to open a file in read mode.

Run
#include<stdio.h>
int main(){
   FILE * nfile;
   if (nfile = fopen("hello.txt", "r")){
      printf("File opened successfully in read mode");
   }
   else
   printf("The file is not present! cannot create a new file using r mode");
   fclose(nfile);
   return 0;
}

Output

File opened successfully in read mode

Example 2:

The below program is used to close a file.

Run
#include<stdio.h>
int main(){
   FILE * nfile;
   char string[300];
   if (nfile = fopen("hello.txt", "a+")){
      while(fscanf(nfile,"%s", string)!=EOF){
         printf("%s", string);
      }
      fputs("Hello", nfile);
   }
   fclose(nfile);
   return 0;
}

Output:

//The file has been closed

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription