Home >>C Tutorial >C File Handling

File Handling in C

C File Handling

In order to modify (create, delete, read) the files that are stored through the created C program on the local file system file handling in C is used.

The need of the file handling in C is generated because there is a need of generating some specific input data to a several number of time in the programming and only displaying the data on the console is not enough sometimes. Since, only a limited amount of data can be displayed on the console and we all know that data that is to displayed may be big and as a known fact the memory is volatile hence, recovering the programmatically generated data repeatedly is next to impossible task. But it can be done by storing the data on the local file system that is also known to be volatile and the file can be accessed each and every time. Hence, the need of file handling in C is generated.

Here are the list of operations that can be performed on a file:

  • Reading from the file
  • Deleting the file
  • Writing to the file
  • Opening an existing file
  • Creation of the new file

Functions for file handling in C

Function Description
fopen() This function is used to open a new or existing file
fclose() This function is used to close the file
fseek() This function is used to set the file pointer to given position
fputw() This function is used to write an integer to file
fscanf() This function is used to read data from the file
fprintf() This function is used to write data into the file
fgetw() This function is used to read an integer from file
fputc() This function is used to write a character into the file
ftell() This function is used to return current position
fgetc() This function is used to read a character from file
rewind() This function is used to set the file pointer to the beginning of the file

Let's understand to opening a file in file handling: fopen()

In order to read, write or update a file, it must be opened first. The file is opened by the use of fopen() function. Here is the syntax of the fopen():

FILE *fopen( const char * filename, const char * mode ); 

There are generally two parameters that are accepted by the fopen() function:

  • The first parameter is the file name (string). There is a need to mention the path of the storage where the file is stored if the file is stored at some specific location.
  • Second parameter is the mode in which the file is to be opened. Generally, it is a string.

Here is a list of modes that can be used in the fopen() function:

Mode Description
rb This mode is used to open a binary file in read mode
r+ This mode is used to open a text file in read and write mode
w+ This mode is used to open a text file in read and write mode
a+ This mode is used to open a text file in read and write mode
wb This mode is used to open a binary file in write mode
a This mode is used to open a text file in append mode
r This mode is used to open a text file in read mode
ab This mode is used to open a binary file in append mode
rb+ This mode is used to open a binary file in read and write mode
wb+ This mode is used to open a binary file in read and write mode
w This mode is used to open a text file in write mode
ab+ This mode is used to open a binary file in read and write mode

Let's understand the working of the fopen() function :

  • The first step involves the searching of the file that is to be opened.
  • In the second step, it loads the file from the disk and place it into the buffer. In order to provide the efficiency for the reading operation, buffer is used.
  • In the final step a character pointer is set up that points to the first character of the file that is being searched.

Example of fopen()

#include<stdio.h>  
void main( )  
{  
FILE *fp ;  
char ch ;  
fo = fopen("helloworld.c","r") ;  
while ( 1 )  
{  
ch = fgetc ( fo ) ;  
if ( ch == EOF )  
break ;  
printf("%c",ch) ;  
}  
fclose (fo ) ;  
}  
Output :
#include
void main( )
{
FILE *fp ;
char ch ;
fo = fopen("helloworld.c","r") ;
while ( 1 )
{
ch = fgetc ( fo ) ;
if ( ch == EOF )
break ;
printf("%c",ch) ;
}
fclose (fo ) ;
}

Let's understand to closing a file in file handling: fclose()

As a matter of fact, after all the operation are performed on a file then it must be closed. In order to close a file fclose() function is used.

Here is the syntax for the same:

int fclose( FILE *fp ); 

No Sidebar ads