File
Handling:
Header file used for file handling is fstream.h.
·
It is a process of storing data
into files.
·
File handling transfers data
between program and hard disk file.
·
C++ has its own function to access
file data and store data into files.
Simple
program:
void main()
{
char
*name; int roll_no;
ofstream
fout(“data”);
fout<<”Rahul”;
fout<<101;
fout.close();
ifstream
fin(“data”);
fin>>name;
fin>>roll_no;
cout<<roll_no<<name;
fin.close();
}
File Modes:
File modes specify that, how file data will be read
and write operation perform.
Mode
|
Description
|
|
1.
|
ios::in
|
Open file for read only mode.
|
2.
|
ios::out
|
Open
file for writing mode.
|
3.
|
ios::app
|
Append data to the end of file.
|
4.
|
ios::ate
|
Write
data on the end of file and provide the facility of modifying existing
contents.
|
5.
|
ios::binary
|
Create the binary file.
|
6.
|
ios::nocreate
|
Open
fail if file does not exist.
|
7.
|
ios::noreplace
|
Open fail if file already exist.
|
8.
|
ios::trunc
|
Delete
all the contents of the file if it exists. It will not create new file.
|
Note:
The mode can combine two or more parameter using the bitwise OR (|).
Input and
Output Functions for File handling:
·
Put and Get function:
i.
Put function:
The put function writes a single character to the associated
stream/file.
For Ex:
file.put(c)
Writes a character c to the output buffer at the current put
position and increases the put pointer to point the next character.
ii.
Get function:
The get function read a single character from the associated
stream/file.
For Ex:
file.get(c)
Reads a character c from the current position and increment get
pointer to the next character for next data get.
Sample program for file handling:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
fstream
file;
file.open(“data.txt”,ios::in|ios::out);
char
c[4]={‘a’,’b’,’c’,’d’}, data;
for(int
i=0;c[i]!=NULL;i++)
{
file.put(c[i]);
}
file.seekg(0);
cout<<”\n
Reading data from file\n”;
while(file)
{
file.get(data);
cout<<data;
}
file.close();
}
·
Write and read function:
Write and read functions are
specially designed to input or output binary data in sequence.
file.read((char *)&v,
sizeof(v));
file.write((char *)&v,sizeof(v));
file.read(memory_block,size);
file.write(memory_block,size);
Function
for manipulating file pointer:
C++ provides some functions that relocate the file
pointer and we can set any position inside the file.
i.
seekg():
Move get pointer to specific location.
ii.
seekp():
Move put pointer to specific location.
iii.
tellg():
Give the current position of get pointer.
iv.
tellp():
Give the current position of put pointer.
Reference Positions
|
|
0
|
ios::beg
|
1
|
ios::cur
|
2
|
ios::end
|
Examples:
file.tellp();
file.seekg(offset,reference
position);
file.seekg(0);
Error handling in file operation:
Error that
may occur –
i.
A file which we are trying to open for reading does
not exist.
ii.
The file name used for a new file may already exist.
iii.
We may use an invalid file name.
iv.
There may not any space in the disk for storing more
data.
Error handling functions for file handling –
i.
eof():
Return true, if end of file (EOF) is detected while
reading a file. Otherwise returns false.
ii.
bad():
Return true, if an invalid operation has occurred,
otherwise false.
iii.
good():
Return true, if no error has occurred, otherwise
return fail.
iv.
fail():
Return true, when an input or output operation has
failed.
This comment has been removed by the author.
ReplyDeleteFile Handling in C++
ReplyDeleteFile Handling concept is mainly used for store data permanently system or computer. Using file handling we can store our data in Secondary memory (Hard disk).