Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
34 views

Lecture 3.3.1 File Handling

B The correct syntax to open a file using the open() method is: myfile.open("example.bin", ios::out); Option B is correct. 32

Uploaded by

TEJES
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Lecture 3.3.1 File Handling

B The correct syntax to open a file using the open() method is: myfile.open("example.bin", ios::out); Option B is correct. 32

Uploaded by

TEJES
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

INSTITUTE - UIE

DEPARTMENT- ACADEMIC UNIT-2


Bachelor of Engineering (Computer Science & Engineering)
Subject Name: Object Oriented Programming using C++
Code:22CSH-103

Topic: File handling DISCOVER . LEARN . EMPOWER


Object Oriented
Programming
using C++

Course Objectives

• To enable the students to understand various stages and constructs


of C++ programming language and relate them to engineering
programming problems.
• To improve their ability to analyze and address variety of problems
in programming domains.

2
Course Outcomes
CO Course Outcome
Number

CO1
Understand the concepts of object-oriented programming
including programming process and compilation process.
CO2
Apply different techniques to decompose a problem and
programmed a solution with its sub modules.
CO3
Analyze and explain the behavior of simple programs involving the
programming addressed in the course.
CO4 Implement and evaluate the programs using the syntax and
semantics of object-oriented programming.
CO5
Design the solution of real-world problems in order to determine
that the program performs as expected.

3
Scheme of Evaluation

4
CONTENTS

• Introduction of file
handling
• File operations and
file modes
• Examples

5
What is a File?
• A file is a collection of bytes stored on a secondary storage device,
which is generally a disk of some kind.
• The collection of bytes may be interpreted, for example,
characters, words, lines, paragraphs from a text document;
fields and records belonging to a database;
Or pixels from a graphical image.
• We use files to store data which can be processed by our programs.
• Not only data but our programs are also stored in files

6
NEED FOR DATA FILES
• Many real life problems requires handling of large amount of data.
• Earlier we used arrays to store bulk data.
• The problem with the arrays is that arrays are stored in RAM.
• The data stored in arrays is retained as long as the program is running.
Once the program is over the data stored in the arrays is also lost.
• To store the data permanently we need files.
• Note: Files are required to save our data (on a secondary storage device)
for future use, as RAM is not able to hold our data permanently.

7
INPUT/OUTPUT IN C++ STREAMS
• The input/output system of C++ handles file I/O operations in the same
way it handles console I/O operations.
• It uses file stream as an interface between programs and files.
• A stream is defined as the flow of data.
• Different kinds of stream are used to represent different kinds of data
flow.
• Output stream: The stream which controls the flow of data from the
program to file is called output stream.
• Input stream: The stream which controls the flow of data from the file to
the program is called input stream.
8
INPUT/OUTPUT IN C++ STREAMS

9
INPUT/OUTPUT IN C++ STREAMS
• Each stream is associated with a particular
• class which contains definitions and methods for dealing with that
particular kind of data
• These include fstream, ifstream and ofstream. These classes are
defined in the header file fstream.h. Therefore it is necessary to
include this header file while writing file programs.
• The classes contained in fstream.h are derived from iostream.h. Thus
it is not necessary to include iostream.h in our program, if we are
using the header file fstream.h in it.

10
INPUT/OUTPUT IN C++ STREAMS

11
INPUT/OUTPUT IN C++ STREAMS
CONTD…
• The ifstream class contains open() function with default input mode
and inherits the functions get(), getline(), read(), seekg() and tellg().
• The ofstream class contains open() function with default output
mode and inherits functions put(), write(), seekp() and tellp() from
ostream.
• The fstream class contains open() function with default input/output
mode and inherits all I/O functions from iostream.h.

12
TYPES OF DATA FILES
There are two types of data files in C++: Text files and Binary files

• Text files store the information in ASCII characters. Each line of text in
text files is terminated by a special character called EOL. In text files
some internal translations take place while storing data.

• Binary files store information in binary format. There is no EOL


character in binary files and no character translation takes place in
binary files.

13
DIFFERENCE BETWEEN TEXT FILES
AND BINARY FILES

14
DIFFERENCE BETWEEN TEXT FILES
AND BINARY FILES CONTD…

15
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()

16
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.
17
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 | .

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

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

20
Program Explanation
• 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 create a
new file and print “New file created”.

21
Example 2: opening/creating a file using the
open() function
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream my_file;
my_file.open("my_file", ios::out);
if (!my_file) {
cout << "File not created!";
}
else {
cout << "File created successfully!";
my_file.close();
}
return 0;} 22
Output

23
Screenshot of the code:

24
Explanation of the Program

25
Close a File
It is simply done with the help of close() function.
Syntax: File Pointer.close()

26
Close a File
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream new_file;
new_file.open("new_file.txt",ios::out);
new_file.close();
return 0;
}
Output:
The file gets closed.
27
Applications
• 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.
• Reusability: It helps in preserving the data or information generated
after running the program.
• Large storage capacity: Using files, you need not worry about the
problem of storing data in bulk.

28
Summary

In this lecture we have We have discussed about File


discussed about File Handling. Operations and File modes

Discussed about examples of


Opening and Closing a file

29
Frequently Asked question
Q1:It is not possible to combine two or more file opening mode in open ()
method.
A. TRUE
B. FALSE
C. May Be
D. Can't Say
Ans: B

30
Q2: Which of the following methods can be used to open a file in file
handling?

A. Using Open ( )
B. Constructor method
C. Destructor method
D. Both A and B
Ans: D

31
Q3: Which is correct syntax ?
A. myfile:open ("example.bin", ios::out);
B. myfile.open ("example.bin", ios::out);
C. myfile::open ("example.bin", ios::out);
D. myfile.open ("example.bin", ios:out);
Ans: B

32
Assessment Questions:
1. ios::trunc is used for ?

A. If the file is opened for output operations and it already existed, no action is taken.
B. If the file is opened for output operations and it already existed, then a new copy is created.
C. If the file is opened for output operations and it already existed, its previous content is deleted
and replaced by the new one.
D. None of the above

2. Which of the following true about FILE *fp

A. FILE is a structure and fp is a pointer to the structure of FILE type


B. FILE is a buffered stream
C. FILE is a keyword in C for representing files and fp is a variable of FILE type
D. FILE is a stream

33
3 . Which of the following is used to create an output stream?
a) ofstream
b) ifstream
c) iostream
d) fsstream

4. Which header file is required to use file I/O operations?


a) <ifstream>
b) <ostream>
c) <fstream>
d) <iostream>

5. Which of the following is not used as a file opening mode?


a) ios::trunc
b) ios::binary
c) ios::in
d) ios::ate

34
Discussion forum.

TRY THIS!!
Given that a binary file “student.dat” is already loaded in
the memory of the computer with the record of 100
students, the task is to read the Kth record and perform
some operations.

35
REFERENCES
Reference Books
[1] Programming in C by Reema Thareja.
[2] Programming in ANSI C by E. Balaguruswamy, Tata McGraw Hill.
[3] Programming with C (Schaum's Outline Series) by Byron Gottfried  Jitender
Chhabra, Tata McGraw Hill.
[4] The C Programming Language by Brian W. Kernighan, Dennis Ritchie, Pearson
education.

Websites:
 https://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm
 https://www.edureka.co/blog/file-handling-in-cpp/
 https://www.geeksforgeeks.org/file-handling-c-classes/

YouTube Links:
What is File Handling?
https://spoken-tutorial.org/watch/C+and+Cpp/File+Handling+In+C/ 36
English/
THANK YOU

You might also like