File Handling Notes Part 1
File Handling Notes Part 1
----------------------
File handling is used to store data permanently in computer. Using file handling we can store our data
in secondary memory (Hard disk) device.
Naming a file
Opening a file
Writing data into the file
Reading data from the file
Closing a file.
Streams in C++
We give input to the executing program and the execution program gives back the output. The
sequence of bytes given as input to the executing program and the sequence of bytes that comes as
output from the executing program are called stream. In other words, streams are nothing but the
flow of data in a sequence of bytes.
Keyboard Monitor
RAM
(Random access
memory)
Output Stream(ofstream)
FILE
ios
iostream file
iostream
-----------------------------------------------------------------------------------------------------------------------------------
fstream file
fstreambase
ios
This class is the base class for other classes in this class hierarchy.
This class contains the necessary facilities that are used by all the other derived classes
for input and output operations.
istream
This class is derived from the class ‘ios’.
This class handle input stream.
The extraction operator(>>) is overloaded in this class to handle input streams from files
to the program execution.
This class declares input functions such as get(), getline() and read().
ostream
This class is derived from the class ‘ios’.
This class handle output stream.
The insertion operator(<<) is overloaded in this class to handle output streams to files
from the program execution.
This class declares output functions such as put() and write().
streambuf
This class contains a pointer which points to the buffer which is used to manage the
input and output streams.
fstreambase
This class provides operations common to the file streams. Serves as a base for fstream,
ifstream and ofstream class.
This class contains open() and close() function.
ifstream
This class provides input operations.
It contains open() function with default input mode.
Inherits the functions get(), getline(), read(), seekg() and tellg() functions from the
istream.
ofstream
This class provides output operations.
It contains open() function with default output mode.
Inherits the functions put(), write(), seekp() and tellp() functions from the ostream.
fstream
This class provides support for simultaneous input and output operations.
Inherits all the functions from istream and ostream classes through iostream.
filebuf
Its purpose is to set the file buffers to read and write.
Opening and closing a File
------------------------------------------
If we want to use a disk file, we need to decide the following things about the file and its
intended use:
1. Suitable name for the file
2. Data type and structure
3. Purpose
4. Opening method
The file name is a string of characters that make up a valid file name for the operating system.
It may contain two parts, a primary name and an optional period with extension.
Eg:
Student.doc
Item.txt
For opening a file, we must first create a file stream and then link it to the file name. A file
stream can be defined using the class’s ifstream, ofstream and fstream. The class to be used
depends upon the purpose i.e. whether we want to read data from the file or write data to it.
Note: The first method is useful when we use only one file in the stream. The second method is
used when we want to manage multiple files using one stream.
1. Create a file stream object to manage the stream using the appropriate class. That is the
class ofstream is used to create the output stream and the class ifstream is used to create
input stream.
2. Initialize the file object with the desired file name
Eg:
File_stream_class <stream-object>(“file-name”);
ofstream fout(“student.doc”);
This create “fout” as an ofstream object that manages the output stream. The object can
be any valid C++ name such as o_file, myfile, outfile etc. This statement opens the file
“student.doc” and attaches it to the output stream “fout”.
Similarly, the following statements declares “fin” as an ifstream object and attaches it to
the file “student.doc” for reading.
Eg:
ifstream fin(“student.doc”);
The connection with a file is closed automatically when the stream object expires mean when
the program terminates.
Program 2
get data
fin
Q: WA C++ file handling program to write data into the file called student.doc
#include<iostream>
#include<fstream>
using namespace std;
main()
{
int rno,fee;
char name[50];
cout<<"Enter the Roll Number:";
cin>>rno;
cout<<"\nEnter the Name:";
cin>>name;
cout<<"\nEnter the Fee:";
cin>>fee;
ofstream fout("d:/student.doc");
fout.close();
return 0;
}
Q: WA C++ file handling program to read data from the file called student.doc
#include<iostream>
#include<fstream>
using namespace std;
main()
{
int rno,fee;
char name[50];
ifstream fin("d:/student.doc");
fin.close();
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
return 0;
}
Q: write a single file handling program in c++ to reading and writing data on a file.
#include<iostream>
#include<fstream>
using namespace std;
main()
{
int rno,fee;
char name[50];
cout<<"Enter the Roll Number:";
cin>>rno;
cout<<"\nEnter the Name:";
cin>>name;
cout<<"\nEnter the Fee:";
cin>>fee;
ofstream fout("d:/student.doc");
fout.close();
ifstream fin("d:/student.doc");
fin.close();
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
return 0;
}