File handling in c means we can create,open , delete files and modify contents in file.For this operations we will use some file handling functions. fopen, fclose, fscanf, fprintf ,fgets, fputs, feof etc.
File operations follow this procedure :
name of file
open or create file
read or write on file.
close file
File Handling Modes :
File Mode
|
Description |
r
|
Opens a file in read only mode. |
w
|
Opens a file in write mode , it creates a new file with provided file name if it doesnot exist.If file exists then file will be overwritten. |
a
|
Opens a file in append mode ( new info will be added at end of file), If file doesnot exist then it will create a new file. |
r+
|
Opens a file for read and write mode. |
w+
|
Opens a file for read and write mode.It creates a new file if it doesnot exist. |
a+
|
Opens a file for read and write mode. It create a new file if it doesnot exist. |
rb
|
Opens a binary file for reading. |
wb
|
Opens a binary file for writing.If file exist it will be over written. |
ab
|
Opens a binary file in append mode.Info is added at end of file. |
r+b or rb+
|
Opens binary file for reading and writing |
w+b or wb+
|
Opens a binary file for read and write mode.It creates a new file if it doesnot exist. |
a+b or ab+
|
Opens a binary file for read and write mode. It create a new file if it doesnot exist. |
File handling functions :
fopen : To open or create file
|
FILE *fopen(const char *filename,const char *mode) |
fclose : To close a open file, we need file pointer of opened file.
Where fp is file pointer.
fscanf : To read from file according to format provided.
|
int fscanf ( FILE * stream, const char * format, ... ); |
fprintf : To write into file according to format provided.
|
int fprintf ( FILE * stream, const char * format, ... ); |
feof : To check end of file.
|
int feof ( FILE * stream ); |
fgets : Reads a line from file and stores in string.
|
char * fgets ( char * str, int num, FILE * stream ); |
fputs: Writes a string into file.
|
int fputs ( const char * str, FILE * stream ); |
fgetc: Reads a single character from file into char variable.
|
int fgetc ( FILE * stream ); |
fputc: Writes a single character to file.
|
int fputc (int c,FILE * stream ); |
Example :
Program to copy content of one file into other file.
Given:
Source filename :src.txt
Destination filename: dst.txt
|
#include<stdio.h> int main(){ FILE *sfp,*dfp; char c; sfp=fopen("src.txt","r"); dfp=fopen("dst.txt","w"); while(!feof(sfp)){ c=fgetc(sfp); fputc(c,dfp); } fclose(sfp); fclose(dfp); return 0; } |
Explanation :
Above code copies content of one txt file into other txt file.
sfp & dfp are file pointers.
src file is opened in read mode and dst file is is opened in write mode.
We will stop copying content when source file(src.txt) file pointer reaches to EOF (EOF is end of file which indicates that we have reached at end of file ).
we are using fgetc function to get char from src file. We are storing fetched content from source file into character variable c, then we are storing back the value of character variable c into the destination file using fputc function.
After copying the content we are closing the opened files.