[go: up one dir, main page]

0% found this document useful (0 votes)
46 views26 pages

Institute - Uie Department-Academic Unit-1

This document discusses file handling in C++. It covers opening files using ifstream, ofstream and fstream classes. Modes like ios::in, ios::out and ios::app can be used for opening files. Functions like open(), read(), write() and close() allow performing file operations. The document also discusses random access of files using seekg(), seekp(), tellg() and tellp() functions.

Uploaded by

Prince Kumar
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)
46 views26 pages

Institute - Uie Department-Academic Unit-1

This document discusses file handling in C++. It covers opening files using ifstream, ofstream and fstream classes. Modes like ios::in, ios::out and ios::app can be used for opening files. Functions like open(), read(), write() and close() allow performing file operations. The document also discusses random access of files using seekg(), seekp(), tellg() and tellp() functions.

Uploaded by

Prince Kumar
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
You are on page 1/ 26

INSTITUTE - UIE

DEPARTMENT- ACADEMIC UNIT-1


Bachelor of Engineering (Computer Science & Engineering)
Subject Name and Code: Object-oriented Programming using
C++
CSP-157
Prepared by Ms. Jasleen Kaur
File Handling DISCOVER . LEARN . EMPOWER
CHAPTER -9
Course Outcome
CO Title Level
Number

CO1 provide the environment that allows students to Remember


understand object-oriented programming Concepts.   Will be covered in this
CO2 demonstrate basic experimental skills for Understand lecture
differentiating between object-oriented and  
procedural programming paradigms and the
advantages of object-oriented programs.

CO3 Ability to demonstrate their coding skill on complex Understand


programming concepts and use it for generating
solutions for engineering and mathematical problems.
CO4 Students will develop skills to design the application of Understand
classes, objects, constructors, destructors, inheritance,  
operator overloading and polymorphism, pointers,
virtual functions, templates, exception handling, file
operations and handling
2
• Introduction to File streams,
• Hierarchy of file stream classes,
• File operations,
CONTENTS • File I/O,
• File opening Modes,
• Reading/Writing of files,
• Random-access to files.

3
INTRODUCTION
Working with files generally requires the following kinds of data communication methodologies:
• Data transfer between console units
• Data transfer between the program and the disk file
So far we have learned about iostream standard library which provides cin and cout methods for reading from standard input
and writing to standard output respectively. In this chapter, you will get to know how files are handled using C++ program and
what are the functions and syntax used to handle files in C++.

4
FILE HANDLING
A C++ stream is a flow of data into or out of a program, such as the data written to cout or read from cin.
For this class we are currently interested in four different classes:
• istream is a general purpose input stream. cin is an example of an istream.
• ostream is a general purpose output stream. cout and cerr are both examples of ostreams.
• ifstream is an input file stream. It is a special kind of an istream that reads in data from a data file.
• ofstream is an output file stream. It is a special kind of ostream that writes data out to a data file.

5
FILE HANDLING
• Files are used to store data in a storage device permanently.
• File handling provides a mechanism to store the output of a program in a file and to perform various operations on it.
• A stream is an abstraction that represents a device on which operations of input and output are performed. A stream can be
represented as a source or destination of characters of indefinite length depending on its usage.
• In C++ we have a set of file handling methods. These include ifstream, ofstream, and fstream. These classes are derived
from fstrembase and from the corresponding iostream class. These classes, designed to manage the disk files, are declared
in fstream and therefore we must include fstream and therefore we must include this file in any program that uses files.

6
STREAM CLASS HEIRARCHY
• Files are used to store data in a storage device permanently.

Figure 9.1 File stream hierarchy [2] 7


FILE HANDLING
C++ provides us with the following operations in File Handling:
• Creating a file: open()
• Reading data: read()
• Writing new data: write()
• Closing a file: close()

8
OPENING A FILE
Generally, the first operation performed on an object of one of these classes is to associate it to a real file. This procedure is
known to open a file.
We can open a file using any one of the following methods:
1. First is by passing the file name in constructor at the time of object creation.
2. Second is using the open() function.
To open a file, use open() function
Syntax
void open(const char* file_name,ios::openmode mode);

Here, the first argument of the open function defines the name and format of the file with the address of the file.
The second argument represents the mode in which the file has to be opened.

9
OPENING A FILE- OPENING MODES
The following modes are used as per the requirements.

Example:
fstream new_file;
new_file.open(“newfile.txt”,
ios::out);

Default Open Modes :


ifstream ios::in
ofstream ios::out
fstream ios::in | ios::out

We can combine the different


modes using or symbol | .

Figure 9.1 File opening modes [1] 10


Example of opening/creating a file using the
open() function

Figure 9.2 Program [1] 11


Example of opening/creating a file using the
open() function

Figure 9.3 Program output [1]

• In the above example we first create an object to class fstream and name it ‘new_file’.
• Then we apply the open() function on our ‘new_file’ object.
• We give the name ‘new_file’ to the new file we wish to create and we set the mode to ‘out’ which allows us to write in our file.
• We use a ‘if’ statement to find if the file already exists or not if it does exist then it will going to print “File creation failed” or
it will gonna create a new file and print “New file created”.

12
Example of writing to a file using the open()
function

Explanation
Here we first create a new file
“new_file_write” using open()
function since we wanted to
send output to the file so, we
use ios::out. As given in the
program, information typed
inside the quotes after
Insertion Pointer “<<” got
passed to the output file.

Figure 9.4 Program [1] 13


PROGRAM-2
• #include <iostream> • int main()
• using namespace std; • {
• class Random • Random* a = new Random[3];
• { • delete [] a; // Delete array
• public: • return 0;
• Random() { • }
• cout << "Constructor" << endl;
• }
• ~Random() {
• cout << "Destructor" << endl;
• }
• };

Figure 945 Program made on Dev-C++[1]


14
READING FROM FILE
• .

Figure 9.5 Program made on Dev-C++


15
READING FROM FILE
• .

Figure 9.5 Program made on Dev-C++


16
RANDOM ACCESS IN FILES
• In C++, random access is achieved by manipulating seekg(), seekp(), tellg() and tellp() functions.
• The seekg() and tellg() functions allow you to set and examine the get_pointer, and the seekp() and tellp() functions perform
these operations on the put_pointer.
• The seekg() and tellg() functions are for input streams (ifstream) and seekp() and tellp() functions are for output streams
(ofstream). However, if you use them with an fstream object then tellg() and tellp() return the same value. Also seekg() and
seekp() work the same way in an fstream object.
• The working of seekg() & seekp() and tellg() & tellp() is just the same except that seekg() and tellg() work for ifstream
objects and seekp() and tellp() work for ofstream objects. In the above table, seek_dir takes the definition enum seek_dir
{ beg, cur, end};.

ifstream fin;
ofstream fout; // file opening routine
fin.seekg(30); // will move the get_pointer (in ifstream) to byte number 30 in the file
fout.seekp(30); // will move the put_pointer (in ofstream) to byte number 30 in the file

Figure 9.5 Program made on Dev-C++


17
SPECIAL OPERATIONS

Figure 9.6 Random access [2] 18


EXAMPLE

Figure 9.7 Program made on Dev-C++ 19


EXAMPLE

Figure 9.8 Program made on Dev-C++ 20


EXAMPLE

Figure 9.9 Program made on Dev-C++ 21


EXAMPLE-OUTPUT

Figure 9.10 Program made on Dev-C++ 22


Assessment Pattern
Section-A
1. (a) Define tellg() and tellp() functions in C++.
(b) What are file opening modes?
(c) Differentiate between opening a file using open function and constructor.
Section-B
2. Write a C++ program to demonstrate the concept of random access in files.
3. WAP in C++ to demonstrate reading from a file.
4. Write a C++ program to write set of lines to a file.

23
APPLICATIONS

• File handling functions are greatly used in building real life projects.

24
REFERENCES

• Reference Website
[1] https://www.edureka.co/blog/file-handling-in-cpp/
[2] https://www.studytonight.com/cpp/file-streams-in-cpp.php
[3] http://www.infobrother.com/Tutorial/C++/C++-Random-files
[4] https://codescracker.com/cpp/cpp-file-pointers-random-access.htm

25
THANK YOU

For queries
Email: CST157_2019@gmail.com

You might also like