File Handling in c language
File : It is a physical memory space on the Hard Disk. It contains data in some format ( text format / binary format ) . Files are used to store the data permanently.
File operations.
1) Opening
a File.
2)
Reading
data from the file
3)
Writing
data to the file
4)
Closing
a file.
File Management
functions:
<stdio.h> header file provides below functions to manage the files
1.
fopen()
Ã
to open already existing file for read or write or append and also to create
new file
2.
fclose()
Ã
to close the file which is already opened for read or write or append
3.
fgetc()
and getc() Ã to read character from the file
4.
fputc()
and putc() Ã to write a character to the file
5.
fgetw() and getw() Ã
to read an integer from the file
6.
fputw() and putw() Ã
to write an integer to the file
7.
fscanf()
Ã
to read formatted data from the file like scanf() from keyboard
8.
fprintf() Ã to write
formatted data to the file
9.
sscanf() Ã to read formatted
data from string buffer
10.
spritnf() Ã to write formatted
data to string buffer
11.
fread() Ã to read
structured data from the file
12.
fwrite() Ã to write
structured data to the file
13.
fseek() Ã to place the file
pointer at required position in a file ( random access )
14.
rewind() Ã to place the file
pointer at the beginning of a file
15.
ftell() Ã returns current
position of file pointer in a file
16.
remove() Ã to delete a file
17.
rename ()
Ã
to rename a file
Working
with fopen():
FILE * fopen ( char *file_path,
char * mode);
FILE Ã It is shortcut name given to the file structure from
<stdio.h>
FILE * Ã
It is a return type of fopen() function. it is pointer to file buffer.
fopen()
function creates a temporary file buffer in the ram memory and transfers data
from hard disk to ram file buffer and return address of that buffer.
On
successfully opening a file fopen() returns a pointer to file ( address )
If
fail to open a file it return NULL pointer
NULL
is a pre-defined constant from <stdio.h>
#define
NULL 0
Standard
file pointers:
1.
FILE
*stdin à to refer to input buffer
2.
FILE
*stdout à to refer to output buffer
3.
FILE
*stderr à to refer to error
buffer
file_path à It is a location of the file
Eg:
“C:\\project\\test\\file1” Ã
absolute path of the file
“./file1” Ã current working directory
fopen() Ã default format opening file is
text format
mode: It is a purpose of opening a file.
These
are of three types.
1. Read mode
2. Write mode
3. Append mode
Read mode :
t à text file
b à binary file
“r” (or)
“rt” Ã read only text file
“rb” Ã read only binary file
“r+” (or)
“rt+” Ã to open text file read mode and also to support write
as well as
append
“rb+” Ã
to open binary file in read mode and also to support write and append.
Eg:
FILE *fp;
fp =
fopen(“c:\\techchannelwithdsk\\test\\file1.txt”,”r”);
if file is already exist
then fopen() opens the file and place the file pointer at the beginning of the
file and returns address.
if file does not exist
then fopen() returns null pointer ( NULL
)
if(fp==NULL)
{
printf(“\n file not found”);
}
NULL is pre-defined
constant from <stdio.h> header file.
#define NULL 0
Write mode :
“w” (or) “wt” Ã write
only text file
“wb” Ã write only binary file
“w+” (or) “wt+” Ã
to open text file in write mode and also
to support read as well as append
“wb+” Ã
to open binary file in write mode and also to support read and append.
In write mode if
file already exist then fopen() over writes the file data.
If file does not
exist then fopen() creates new file.
Append mode :
“a” (or)
“at” Ã append only text file
“ab” Ã append only binary file
“a+” (or) “at+” Ã
to open text file append mode and also
to support read as well as write
“ab+” Ã
to open binary file in append mode and also to support read and write.
à In append mode if file already
exist then fopen() opens the file place the file pointer at the end of the file.
à If file does not exist then fopen()
creates new file.
Suggested coding :
When expecting to open existing
file, use read mode
When expecting to create
a new file, use write mode
0 Comments