SlideShare a Scribd company logo
2
Most read
3
Most read
Streams and Files:
Chapter 12:
Ms. Munazza Mah Jabeen
Assistant Professor of Computer Science
Stream Classes:
• A stream is a general name given to a flow of data.
iostream standard library provide cin and cout methods
for reading from standard input and writing to standard
output respectively.
• To read and write from a file we need another standard
C++ library called fstream, which defines three new data
types:
• ofstream
• ifstream
• fstream
• To perform file processing in C++, header files <iostream>
and <fstream> must be included in your C++ source file
• The extraction operator >> is a
• member of the istream class, and the insertion operator
<< is a member of the ostream class.
Opening / Closing a File:
• A file must be opened before you can read from it or write to it. Either
the ofstream or fstream object may be used to open a file for writing
and ifstream object is used to open a file for reading purpose only.
standard syntax for open() function:
void open(const char *filename, ios::openmode mode)
To open a file in write mode or truncate it if it exists
ofstream outfile;
outfile.open("file.dat", ios::out | ios::trunc );
fstream afile;
afile.open("file.dat", ios::out | ios::in )
• To close a File:
standard syntax for close() function
void close();
Reading / Writing a File:
• In order to write to a file we use the
stream insertion operator (<<) just
sending output to the screen. The
only difference is that you use an
ofstream or fstream object instead
of the cout object.
• To read information from a file use
the stream extraction operator (>>)
just like input information from the
keyboard. The only difference is that
you use an ifstream or fstream
object instead of the cin object.
Strings with Embedded Blanks:
Object I/O:
I/O with Multiple Objects:
Error Handling in File I/O:

More Related Content

PPTX
File handling
muhammad sharif bugti
 
PDF
Chapter28 data-file-handling
Deepak Singh
 
PPTX
Files in c++
NivethaJeyaraman
 
PDF
C++ Files and Streams
Ahmed Farag
 
PPTX
CSV File Manipulation
primeteacher32
 
PPTX
Reading and Writing Files
primeteacher32
 
PPTX
2CPP17 - File IO
Michael Heron
 
PPTX
file management functions
vaani pathak
 
File handling
muhammad sharif bugti
 
Chapter28 data-file-handling
Deepak Singh
 
Files in c++
NivethaJeyaraman
 
C++ Files and Streams
Ahmed Farag
 
CSV File Manipulation
primeteacher32
 
Reading and Writing Files
primeteacher32
 
2CPP17 - File IO
Michael Heron
 
file management functions
vaani pathak
 

What's hot (20)

PPTX
File handling in vb.net
Everywhere
 
PPTX
Chapter 10.3
sotlsoc
 
PPTX
Filehandling
Amandeep Kaur
 
PPTX
File handling
Amber Wajid
 
PPT
File handling
prateekgemini
 
PPT
Java File I/O
Canterbury HS
 
PPTX
31cs
Sireesh K
 
PDF
Filehadnling
Khushal Mehta
 
PPTX
Stream classes in C++
Shyam Gupta
 
PPTX
File Handling
TusharBatra27
 
PPTX
[Java] #7 - Input & Output Stream
Ghadeer AlHasan
 
PPTX
Data file handling in c++
Vineeta Garg
 
PPT
Filehandlinging cp2
Tanmay Baranwal
 
PPTX
File handling in c++
ProfSonaliGholveDoif
 
PPT
14 file handling
APU
 
PPTX
Data file handling in python binary & csv files
keeeerty
 
PPT
File Input & Output
PRN USM
 
PPT
ASP.NET Session 7
Sisir Ghosh
 
File handling in vb.net
Everywhere
 
Chapter 10.3
sotlsoc
 
Filehandling
Amandeep Kaur
 
File handling
Amber Wajid
 
File handling
prateekgemini
 
Java File I/O
Canterbury HS
 
31cs
Sireesh K
 
Filehadnling
Khushal Mehta
 
Stream classes in C++
Shyam Gupta
 
File Handling
TusharBatra27
 
[Java] #7 - Input & Output Stream
Ghadeer AlHasan
 
Data file handling in c++
Vineeta Garg
 
Filehandlinging cp2
Tanmay Baranwal
 
File handling in c++
ProfSonaliGholveDoif
 
14 file handling
APU
 
Data file handling in python binary & csv files
keeeerty
 
File Input & Output
PRN USM
 
ASP.NET Session 7
Sisir Ghosh
 
Ad

Similar to Streams and Files (20)

PPT
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
PDF
Data file handling
Prof. Dr. K. Adisesha
 
PPTX
Filesin c++
HalaiHansaika
 
PDF
Files and streams
Pranali Chaudhari
 
PPTX
13 file handling in C++.pptx oops object oriented programming
archana22486y
 
PPSX
Files in c++
Selvin Josy Bai Somu
 
PDF
Filesinc 130512002619-phpapp01
Rex Joe
 
PPT
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
AzanMehdi
 
PPT
File handling in_c
sanya6900
 
PPTX
file.pptx 43dcsddsafgdewdvvbghghsdwweffr
abdelhamidatef1
 
PPT
Data file handling
Saurabh Patel
 
PDF
streams and files
Mariam Butt
 
PPTX
Working with files in c++. file handling
tfluid16
 
PPT
file_handling_in_c.ppt......................................
nadoj47203
 
PPT
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
SanskritiGupta39
 
PDF
Files in C++.pdf is the notes of cpp for reference
anuvayalil5525
 
PPTX
Introduction to files management systems
araba8
 
PDF
File handling
Swarup Kumar Boro
 
PPTX
basics of file handling
pinkpreet_kaur
 
PPTX
Basics of file handling
pinkpreet_kaur
 
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
Data file handling
Prof. Dr. K. Adisesha
 
Filesin c++
HalaiHansaika
 
Files and streams
Pranali Chaudhari
 
13 file handling in C++.pptx oops object oriented programming
archana22486y
 
Files in c++
Selvin Josy Bai Somu
 
Filesinc 130512002619-phpapp01
Rex Joe
 
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
AzanMehdi
 
File handling in_c
sanya6900
 
file.pptx 43dcsddsafgdewdvvbghghsdwweffr
abdelhamidatef1
 
Data file handling
Saurabh Patel
 
streams and files
Mariam Butt
 
Working with files in c++. file handling
tfluid16
 
file_handling_in_c.ppt......................................
nadoj47203
 
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
SanskritiGupta39
 
Files in C++.pdf is the notes of cpp for reference
anuvayalil5525
 
Introduction to files management systems
araba8
 
File handling
Swarup Kumar Boro
 
basics of file handling
pinkpreet_kaur
 
Basics of file handling
pinkpreet_kaur
 
Ad

More from Munazza-Mah-Jabeen (20)

PPTX
Virtual Functions
Munazza-Mah-Jabeen
 
PPTX
The Standard Template Library
Munazza-Mah-Jabeen
 
PPTX
Object-Oriented Software
Munazza-Mah-Jabeen
 
PPTX
Templates and Exceptions
Munazza-Mah-Jabeen
 
PPTX
Dictionaries and Sets
Munazza-Mah-Jabeen
 
PPTX
More About Strings
Munazza-Mah-Jabeen
 
PPTX
Lists and Tuples
Munazza-Mah-Jabeen
 
PPT
Files and Exceptions
Munazza-Mah-Jabeen
 
PPT
Functions
Munazza-Mah-Jabeen
 
PPTX
Pointers
Munazza-Mah-Jabeen
 
PPT
Repitition Structure
Munazza-Mah-Jabeen
 
PPTX
Inheritance
Munazza-Mah-Jabeen
 
PPTX
Operator Overloading
Munazza-Mah-Jabeen
 
PPT
Memory Management
Munazza-Mah-Jabeen
 
PPTX
Arrays and Strings
Munazza-Mah-Jabeen
 
PPTX
Objects and Classes
Munazza-Mah-Jabeen
 
PPTX
Functions
Munazza-Mah-Jabeen
 
PPTX
Structures
Munazza-Mah-Jabeen
 
PPTX
Loops and Decisions
Munazza-Mah-Jabeen
 
PPTX
C++ programming basics
Munazza-Mah-Jabeen
 
Virtual Functions
Munazza-Mah-Jabeen
 
The Standard Template Library
Munazza-Mah-Jabeen
 
Object-Oriented Software
Munazza-Mah-Jabeen
 
Templates and Exceptions
Munazza-Mah-Jabeen
 
Dictionaries and Sets
Munazza-Mah-Jabeen
 
More About Strings
Munazza-Mah-Jabeen
 
Lists and Tuples
Munazza-Mah-Jabeen
 
Files and Exceptions
Munazza-Mah-Jabeen
 
Repitition Structure
Munazza-Mah-Jabeen
 
Inheritance
Munazza-Mah-Jabeen
 
Operator Overloading
Munazza-Mah-Jabeen
 
Memory Management
Munazza-Mah-Jabeen
 
Arrays and Strings
Munazza-Mah-Jabeen
 
Objects and Classes
Munazza-Mah-Jabeen
 
Structures
Munazza-Mah-Jabeen
 
Loops and Decisions
Munazza-Mah-Jabeen
 
C++ programming basics
Munazza-Mah-Jabeen
 

Recently uploaded (20)

PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 

Streams and Files

  • 1. Streams and Files: Chapter 12: Ms. Munazza Mah Jabeen Assistant Professor of Computer Science
  • 2. Stream Classes: • A stream is a general name given to a flow of data. iostream standard library provide cin and cout methods for reading from standard input and writing to standard output respectively. • To read and write from a file we need another standard C++ library called fstream, which defines three new data types: • ofstream • ifstream • fstream • To perform file processing in C++, header files <iostream> and <fstream> must be included in your C++ source file • The extraction operator >> is a • member of the istream class, and the insertion operator << is a member of the ostream class.
  • 3. Opening / Closing a File: • A file must be opened before you can read from it or write to it. Either the ofstream or fstream object may be used to open a file for writing and ifstream object is used to open a file for reading purpose only. standard syntax for open() function: void open(const char *filename, ios::openmode mode) To open a file in write mode or truncate it if it exists ofstream outfile; outfile.open("file.dat", ios::out | ios::trunc ); fstream afile; afile.open("file.dat", ios::out | ios::in ) • To close a File: standard syntax for close() function void close();
  • 4. Reading / Writing a File: • In order to write to a file we use the stream insertion operator (<<) just sending output to the screen. The only difference is that you use an ofstream or fstream object instead of the cout object. • To read information from a file use the stream extraction operator (>>) just like input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object.
  • 7. I/O with Multiple Objects:
  • 8. Error Handling in File I/O: