0% found this document useful (0 votes)
19 views16 pages

File Handling in C++ Explained

The document provides an overview of file handling in C++, explaining the concepts of files and streams, and differentiating between text and binary files. It outlines the classes used for file operations, such as ifstream, ofstream, and fstream, and describes methods for opening files. Additionally, it discusses the steps necessary for file operations and how to manage multiple files using streams.

Uploaded by

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

File Handling in C++ Explained

The document provides an overview of file handling in C++, explaining the concepts of files and streams, and differentiating between text and binary files. It outlines the classes used for file operations, such as ifstream, ofstream, and fstream, and describes methods for opening files. Additionally, it discusses the steps necessary for file operations and how to manage multiple files using streams.

Uploaded by

yathavi05052007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

WORKING WITH

FILES
File Handling in C++

Computer programs work with files -as it helps in


storing data & information permanently.
File: is a bunch of bytes stored under a specific name
on a storage device.
Stream: It refers to a sequence of bytes.
Every file is linked to a stream & each stream is
associated with particular class.
File Handling in C++
TYPES OF FILES

Types of files

TEXT FILE BINARY FILE


TEXT FILES
Text file:-
1. It is a file that stores information in ASCII
characters.
2. In text files, each line of text is terminated with a
special character known as EOL (End of Line)
character or delimiter character.
3. When this EOL character is read or written, certain
internal translations take place.
BINARY FILES
Binary file:-
1. It is a file that contains information in the same
format as it is held in memory.
2. In binary files, no delimiters are used for a line and
no translations occur here.
3. Binary files are faster and easier for programs to
read & write. By default files in c++ are considered
as text files.
CLASSES FOR FILE STREAM OPERATORS

• The I/O system of C++ contains a set


of classes that defines the file
handling methods.
• These include ifstream, ofstream
and fstream.
• These classes are derived from
fstreambase and form the
corresponding iostream class.
• These classes ,designed to manage
the disk files are declared in fstream
Stream classes for file operation
Details of file file stream
classes
STEPS FOR FILE OPERATIONS
• For using a disk file the following things are
necessary :

»Suitable name of file


»Data type and structure
»Purpose
»Opening Method
WAYS OF OPENING A FILE
• A file can be opened in two
ways:

• Using the constructor function of class.


• Using the member function open() of the
class.
The first method is useful only when one file is used in
the [Link] second method is
used when multiple files are to be managed using one
stream.
OPENING A FILE USING CONSTRUCTOR
• Create a file stream object to manage the
stream using the appropriate class
i.e the class ofstream is used to create the
output stream and the class ifstream to
create the input stream.
• Initialize the file object using desired file name.
• For Example :-
ofstream outfile (“results”); //output only
• This create outfile as an ofstream object that
manages the output stream.
• This object can be valid C++ name as
o_file,myfile or fout.
TWO FILE STREAMS ARE SEPERATED FILES
OPENING A FILE
1. Opening file using constructor :
ofstream outFile("[Link]"); ifstream inFile(“[Link]”);

2. Opening File Using open () :


[Link](“filename”, [mode]);
ofstream oFile;
[Link](“temp”,ios::out);
//is same as
[Link](“temp");// by default
ifstream inFile;

[Link]("[Link]“,ios::in);
//is same as
TWO FILE STREAMS WORKING ON ONE FILES
• We can use the same file for both reading and writing.
Program 1
……..
……..
ofstream outfile (“salary”); //creates
outfile
…….. and connects
…….. //”salary” to it
Program 2
……..
……..
ifstream infile (“salary”); //creates infile
…….. and connects
…….. //”salary” to it
Program 1
………….
…………. outfile
Outfile putdata
Program 2 SALARY FILE

………….
………….
infile getdata

• In the above statement, when the program 1 is


terminated,the salary file will be disconnected from the
outfile stream.

You might also like