SlideShare a Scribd company logo
1 of 8
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

What's hot

Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
sotlsoc
ย 
Filehandling
FilehandlingFilehandling
Filehandling
Amandeep Kaur
ย 
File handling
File handlingFile handling
File handling
prateekgemini
ย 
14 file handling
14 file handling14 file handling
14 file handling
APU
ย 
File Input & Output
File Input & OutputFile Input & Output
File Input & Output
PRN USM
ย 
ASP.NET Session 7
ASP.NET Session 7ASP.NET Session 7
ASP.NET Session 7
Sisir Ghosh
ย 

What's hot (20)

File handling in vb.net
File handling in vb.netFile handling in vb.net
File handling in vb.net
ย 
File handling in c++
File handling in c++File handling in c++
File handling in c++
ย 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
ย 
Filehandling
FilehandlingFilehandling
Filehandling
ย 
File handling
File handlingFile handling
File handling
ย 
File handling
File handlingFile handling
File handling
ย 
Java File I/O
Java File I/OJava File I/O
Java File I/O
ย 
31cs
31cs31cs
31cs
ย 
Filehadnling
FilehadnlingFilehadnling
Filehadnling
ย 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
ย 
File Handling
File HandlingFile Handling
File Handling
ย 
[Java] #7 - Input & Output Stream
[Java] #7 - Input & Output Stream[Java] #7 - Input & Output Stream
[Java] #7 - Input & Output Stream
ย 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
ย 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
ย 
File handling in c++
File handling in c++File handling in c++
File handling in c++
ย 
File Handling in C++
File Handling in C++File Handling in C++
File Handling in C++
ย 
14 file handling
14 file handling14 file handling
14 file handling
ย 
Data file handling in python binary & csv files
Data file handling in python binary & csv filesData file handling in python binary & csv files
Data file handling in python binary & csv files
ย 
File Input & Output
File Input & OutputFile Input & Output
File Input & Output
ย 
ASP.NET Session 7
ASP.NET Session 7ASP.NET Session 7
ASP.NET Session 7
ย 

Similar to Streams and Files

basics of file handling
basics of file handlingbasics of file handling
basics of file handling
pinkpreet_kaur
ย 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
pinkpreet_kaur
ย 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for reference
anuvayalil5525
ย 
Input File dalam C++
Input File dalam C++Input File dalam C++
Input File dalam C++
Teguh Nugraha
ย 
Data file handling
Data file handlingData file handling
Data file handling
TAlha MAlik
ย 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
Rex Joe
ย 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
TAlha MAlik
ย 

Similar to Streams and Files (20)

File management in C++
File management in C++File management in C++
File management in C++
ย 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
ย 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
ย 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
ย 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
ย 
Data file handling
Data file handlingData file handling
Data file handling
ย 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for reference
ย 
Input File dalam C++
Input File dalam C++Input File dalam C++
Input File dalam C++
ย 
chapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfchapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdf
ย 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
ย 
File handling in cpp
File handling in cppFile handling in cpp
File handling in cpp
ย 
Data file handling
Data file handlingData file handling
Data file handling
ย 
Files and streams
Files and streamsFiles and streams
Files and streams
ย 
Data file handling
Data file handlingData file handling
Data file handling
ย 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
ย 
Files in c++
Files in c++Files in c++
Files in c++
ย 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
ย 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
ย 
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSFILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
ย 
File handling
File handlingFile handling
File handling
ย 

More from Munazza-Mah-Jabeen

More from Munazza-Mah-Jabeen (20)

Virtual Functions
Virtual FunctionsVirtual Functions
Virtual Functions
ย 
The Standard Template Library
The Standard Template LibraryThe Standard Template Library
The Standard Template Library
ย 
Object-Oriented Software
Object-Oriented SoftwareObject-Oriented Software
Object-Oriented Software
ย 
Templates and Exceptions
 Templates and Exceptions Templates and Exceptions
Templates and Exceptions
ย 
Dictionaries and Sets
Dictionaries and SetsDictionaries and Sets
Dictionaries and Sets
ย 
More About Strings
More About StringsMore About Strings
More About Strings
ย 
Lists and Tuples
Lists and TuplesLists and Tuples
Lists and Tuples
ย 
Files and Exceptions
Files and ExceptionsFiles and Exceptions
Files and Exceptions
ย 
Functions
FunctionsFunctions
Functions
ย 
Pointers
PointersPointers
Pointers
ย 
Repitition Structure
Repitition StructureRepitition Structure
Repitition Structure
ย 
Inheritance
InheritanceInheritance
Inheritance
ย 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
ย 
Memory Management
Memory ManagementMemory Management
Memory Management
ย 
Arrays and Strings
Arrays and StringsArrays and Strings
Arrays and Strings
ย 
Objects and Classes
Objects and ClassesObjects and Classes
Objects and Classes
ย 
Functions
FunctionsFunctions
Functions
ย 
Structures
StructuresStructures
Structures
ย 
Loops and Decisions
Loops and DecisionsLoops and Decisions
Loops and Decisions
ย 
C++ programming basics
C++ programming basicsC++ programming basics
C++ programming basics
ย 

Recently uploaded

Call Girls in Uttam Nagar (delhi) call me [๐Ÿ”9953056974๐Ÿ”] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [๐Ÿ”9953056974๐Ÿ”] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [๐Ÿ”9953056974๐Ÿ”] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [๐Ÿ”9953056974๐Ÿ”] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
hameyhk98
ย 

Recently uploaded (20)

Call Girls in Uttam Nagar (delhi) call me [๐Ÿ”9953056974๐Ÿ”] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [๐Ÿ”9953056974๐Ÿ”] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [๐Ÿ”9953056974๐Ÿ”] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [๐Ÿ”9953056974๐Ÿ”] escort service 24X7
ย 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
ย 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
ย 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
ย 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
ย 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
ย 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
ย 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
ย 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
ย 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
ย 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
ย 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
ย 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
ย 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
ย 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
ย 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
ย 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
ย 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
ย 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
ย 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
ย 

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: