0% found this document useful (0 votes)
21 views6 pages

File Handling Notes Part 1

The document explains file handling in C++, detailing the steps to achieve it, including naming, opening, writing, reading, and closing files. It introduces the concept of streams and the classes used for file operations, such as ifstream, ofstream, and fstream, which are derived from the ios class. Additionally, it provides example C++ programs for writing to and reading from a file named 'student.doc'.

Uploaded by

c60109771
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views6 pages

File Handling Notes Part 1

The document explains file handling in C++, detailing the steps to achieve it, including naming, opening, writing, reading, and closing files. It introduces the concept of streams and the classes used for file operations, such as ifstream, ofstream, and fstream, which are derived from the ios class. Additionally, it provides example C++ programs for writing to and reading from a file named 'student.doc'.

Uploaded by

c60109771
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

File Handling

----------------------
File handling is used to store data permanently in computer. Using file handling we can store our data
in secondary memory (Hard disk) device.

How to achieve the File Handling

For achieving file handling we need to follow the following steps:-

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

Input Stream Output Stream

Console Input output


operation

RAM
(Random access
memory)

Input Stream (ifstream)

Output Stream(ofstream)

Stud.docx File Input Output operation

FILE

Fig: Console Program File Interaction

Classes for File stream operations


The I/O system of C++ contains a set of classes that define the file handling methods. These include
ifstream, ofstream and fstream classes. These classes area derived from fstream and from the
corresponding iostream class. These classes, designed to manage the disk files, are declared in
fstream header file and therefore we must include this header file in any program that uses files.

ios

iostream file

istream streambuf ostream

iostream

-----------------------------------------------------------------------------------------------------------------------------------

ifstream fstream ofstream filebuf

fstream file
fstreambase

Fig: stream classes for file operations

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.

A file can be opened in two ways-

1. Using the constructor function of the class.


2. Using the member function open() of the class.

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.

Opening files using constructor


------------------------------------------
Constructor is used to initialize an object while it is being created. Filename is used to initialize
the stream object. This involves the following steps—

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 1 put data

fout Student file

Program 2
get data
fin

Fig: Two file streams working on one file

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<<rno<<"\t"<<name<<"\t"<<fee; //write data to the file student

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>>rno>>name>>fee; //read data from the file student

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<<rno<<"\t"<<name<<"\t"<<fee; //write data to the file student

fout.close();

ifstream fin("d:/student.doc");

fin>>rno>>name>>fee; //read data from the file student

fin.close();
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
return 0;
}

You might also like