Monday 28 March 2016

File Handling In C

File Handling in C

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. A list of file functions are given below:
No.FunctionDescription
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. The syntax of fopen() function is given below:
  1. FILE *fopen( const char * filename, const char * mode );  
You can use one of the following modes in the fopen() function.
ModeDescription
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. The syntax of fclose() function is given below:
  1. int fclose( FILE *fp );  

Writing File : fprintf() function

The fprintf() function is used to write set of characters into file.
  1. #include <stdio.h>  
  2. main(){  
  3.    FILE *fp;  
  4.    fp = fopen("file.txt""w");//opening file  
  5.    fprintf(fp, "Hello file by fprintf...\n");//writing data into file  
  6.    fclose(fp);//closing file  
  7. }  

Reading File : fscanf() function

The fscanf() function is used to read set of characters from file.
  1. #include <stdio.h>  
  2. main(){  
  3.    FILE *fp;  
  4.    char buff[255];//creating char array to store data of file  
  5.    fp = fopen("file.txt""r");  
  6.    fscanf(fp, "%s", buff);//reading data of file and writing into char array  
  7.    printf("Data is : %s\n", buff );//printing data of char array  
  8.    fclose(fp);  
  9. }  
Output:
Data is : Hello file by fprintf...

C File Example: Storing employee information

Let's see a file handling example to store employee information as entered by user from console. We are going to store id, name and salary of the employee.
  1. #include <stdio.h>  
  2. void main()  
  3. {  
  4.     FILE *fptr;  
  5.     int id;  
  6.     char name[30];  
  7.     float salary;  
  8.     fptr = fopen("emp.txt""w+");/*  open for writing */  
  9.     if (fptr == NULL)  
  10.     {  
  11.         printf("File does not exists \n");  
  12.         return;  
  13.     }  
  14.     printf("Enter the id\n");  
  15.     scanf("%d", &id);  
  16.     fprintf(fptr, "Id= %d\n", id);  
  17.     printf("Enter the name \n");  
  18.     scanf("%s", name);  
  19.     fprintf(fptr, "Name= %s\n", name);  
  20.     printf("Enter the salary\n");  
  21.     scanf("%f", &salary);  
  22.     fprintf(fptr, "Salary= %.2f\n", salary);  
  23.     fclose(fptr);  
  24. }  
Output:
Enter the id 
1
Enter the name 
sonoo
Enter the salary
120000 
Now open file from current directory. For windows operating system, go to TC\bin directory, you will see emp.txt file. It will have following information.
emp.txt
Id= 1
Name= sonoo
Salary= 120000 


No comments:

Post a Comment