File Handling in C

File Handling in C


Why do we need file handling in C? 


Whenever we write a program, we know that program will temporarily store data and delete it as soon as we return from execution terminal to our code.
So, we use files to save data permanently in system.
When we use files in our program user entered data is stored even when we exit execution terminal i.e. we have a permanent copy of data what user ahs entered.
Now as we know why do we use file handling lets see ways to open a file in C.


Operations we can do on file:


  • Create a file.
  • Open a file.
  • Read from a file.
  • Close a file.
To perform any operation on file we need a file pointer.
File pointer performs communication between file and program.

Types of file handling modes:


Whenever we need to open a file we use these modes:

r: 
This is called read mode. Whenever we need to read data we use this mode. In this mode, file pointer is placed at beginning of file. If file does not exist it will return NULL value.

a: 
This is called append mode. Whenever we need to write data to an existing data then we use this mode. File pointer is placed at end of file. If file does not exist it will be created.

w: 
This is called write mode. Whenever we need to write data we use this mode. In this mode, data is overwritten. File pointer is placed at beginning of file. If file does not exist it will be created.

r+:

Opens file for both reading and writing. If file does not exist it will be created and if file exists it will be overwritten.

a+:
Opens file for both reading and appending. If file does not exist it will be created.

w+:
Opens file for both reading and writing. If file does not exist it will return NULL value.

Opening  a file:


Whenever we need to write or read data from a file we need to open it.
FILE *ptr;                        //Declaration of file pointer
ptr = fopen("Filename","mode");        //Opening a file

e.g: 
FILE *ptr;
ptr = fopen("E:\\learn_c_in_simple_words.txt","a");

Reading from file and writing from file:


fscanf() , fprintf() : These functions are used to used read/write block of data from file.
getc() , putc() : These functions are used to used read/write a character from file.

Closing a file:


Any file which is opened must be closed too.
For closing a file we use fclose()
e.g:
fclose(ptr);

Program to understand file handling:



#include<stdio.h>
int main()
{
int a,newa;
printf("\nEnter one number that you want to store in file: ");
scanf("%d",&a);
FILE *ptr;
ptr=fopen("test.txt","w");
fprintf(ptr,"%d",a);
fclose(ptr);
printf("\nData written in file successfully!!!");
printf("\nNow reading data from file...");
ptr=fopen("test.txt","r");
fscanf(ptr,"%d",&newa);
fclose(ptr);
printf("\nData from file is: %d",newa);
return 0;
}

Output:



Explanation:


Here, we have simply opened a file written user entered data into it and saved it permanently in system with help of files. 
Now, we can access this data at any time as it is available to us.


No comments:

Post a Comment