C File Handling

File Handling in c language is used to open, read, write, search or close file. It is used for permanent storage.

Advantage of File

It will contain the data even after program exit. Normally we use variable or array to store data, but data is lost after program exit. Variables and arrays are non-permanent storage medium whereas file is permanent storage medium.

Functions for file handling

There are many functions in C library to open, read, write, search and close file.

1fopen()opens new or existing file
2fprintf()write data into file
3fscanf()reads data from file
4fputc()writes a character into file
5fgetc()reads a character from file
6fclose()closes the file
7fseek()sets the file pointer to given position
8fputw()writes an integer to file
9fgetw()reads an integer from file
10ftell()returns current position
11rewind()sets the file pointer to the beginning of the file

Opening File

The fopen() function is used to open a file.


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

Modes in the fopen() function.

ropens a text file in read mode
wopens a text file in write mode
aopens a text file in append mode
r+opens a text file in read and write mode
w+opens a text file in read and write mode
a+opens a text file in read and write mode
rbopens a binary file in read mode
wbopens a binary file in write mode
abopens a binary file in append mode
rb+opens a binary file in read and write mode
wb+opens a binary file in read and write mode
ab+opens a binary file in read and write mode

Closing File

The fclose() function is used to close a file.


int fclose( FILE *fp ); 

Writing File : fprintf() function

The fprintf() function is used to write set of characters into file.


#include <stdio.h>  
   FILE *fp;  
   //opening file 
   fp = fopen("file.txt", "w"); 
   //writing data into file 
   fprintf(fp, "Hello file by fprintf...\n"); 
   fclose(fp);//closing file  

Readiing File : fscanf() function

The fscanf() function is used to read set of characters from file.


#include <stdio.h>  
   FILE *fp;  
   //creating char array to store data of file  
   char arr[255];
   //read the file.txt
   fp = fopen("file.txt", "r"); 
   //reading data of file and writing into char array   
   fscanf(fp, "%s", arr);
   //printing data of char array 
   printf("Data is : %s\n", arr ); 

Storing student information in file

The file handling example to store student information as entered by user from console. We are going to store id and name of the student.


#include <stdio.h>  
void main()  
    FILE *fptr;  
    int id;  
    char name[30];  
    /*  open for writing */ 
    fptr = fopen("student.txt", "w+"); 
    if (fptr == NULL)  
        printf("File does not exists \n");  
    printf("Enter the id\n");  
    scanf("%d", &id);  
    fprintf(fptr, "Id= %d\n", id);  
    printf("Enter the name \n");  
    scanf("%s", name);  
    fprintf(fptr, "Name= %s\n", name);    
Enter the id 
Enter the name 
Sachin Yadav

Now open file from current directory. For window go to TC\bin directory, you will see emp.txt file. It will have following information.


Id= 11211855
Name= Sachin Yadav

Share this article on