SlideShare a Scribd company logo
2
Most read
3
Most read
6
Most read
C++ [ FILES AND STREAMS ]
AHMED FARAG
INFORMATION SYSTEM DEPARTMENT,
FACULTY OF COMPUTER AND INFORMATION, MENUFIA UNIVERSITY
FILES AND STREAMS
 So far, we have been using the iostream standard library, which provides 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
FSTREAM
NOTE
 To perform file processing in C++, header files <iostream> and
<fstream> must be included in your C++ source file.
OPENING A FILE
 A file must be opened before you can read from it or write to it. Either
theofstream 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);
OPENING A FILE CON…
 The first argument specifies the name and location of the file to be opened
and the second argument of the open() member function defines the mode
in which the file should be opened.
OPENING A FILE CON…
 You can combine two or more of these values by ORing them together. For
example if you want to open a file in write mode and want to truncate it in case
it already exists, following will be the syntax:
ofstream outfile;
outfile.open("file.dat", ios::out | ios::trunc );
fstream afile;
afile.open("file.dat", ios::out | ios::in );
CLOSING A FILE
 When a C++ program terminates it automatically closes flushes all the
streams, release all the allocated memory and close all the opened files.
But it is always a good practice that a programmer should close all the opened
files before program termination.
 standard syntax for close() function
void close();
WRITING TO A FILE
 While doing C++ programming, you write information to a file from your
program using the stream insertion operator (<<) just as you use that operator
to output information to the screen.The only difference is that you use an
ofstream or fstream object instead of the cout object.
READING FROM A FILE
 You read information from a file into your program using the stream extraction
operator (>>) just as you use that operator to input information from the
keyboard.The only difference is that you use an ifstream or fstream object
instead of the cin object.
READ & WRITE EXAMPLE
 https://ideone.com/gir9Ww
READ & WRITE EXAMPLE
 https://ideone.com/Io5oNw

More Related Content

PDF
Managing I/O in c++
Pranali Chaudhari
 
PDF
Files and streams
Pranali Chaudhari
 
PPTX
Stream classes in C++
Shyam Gupta
 
PPSX
Files in c++
Selvin Josy Bai Somu
 
PDF
Let us c++ yeshwant kanetkar
Yash Kapoor
 
PDF
file handling c++
Guddu Spy
 
PPT
Files in c++ ppt
Kumar
 
PDF
Let us c yashwant kanetkar(1)
OMWOMA JACKSON
 
Managing I/O in c++
Pranali Chaudhari
 
Files and streams
Pranali Chaudhari
 
Stream classes in C++
Shyam Gupta
 
Files in c++
Selvin Josy Bai Somu
 
Let us c++ yeshwant kanetkar
Yash Kapoor
 
file handling c++
Guddu Spy
 
Files in c++ ppt
Kumar
 
Let us c yashwant kanetkar(1)
OMWOMA JACKSON
 

What's hot (20)

PPT
friend function(c++)
Ritika Sharma
 
PPTX
Pure virtual function and abstract class
Amit Trivedi
 
PPTX
Type casting in java
Farooq Baloch
 
PPTX
OOPS Basics With Example
Thooyavan Venkatachalam
 
PPTX
classes and objects in C++
HalaiHansaika
 
PPTX
Inheritance in java
Tech_MX
 
PPTX
Constructor in java
Madishetty Prathibha
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPTX
Static Data Members and Member Functions
MOHIT AGARWAL
 
PPTX
JAVA AWT
shanmuga rajan
 
PPTX
Programming in c Arrays
janani thirupathi
 
PPTX
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
PPTX
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
PPT
Input and output in C++
Nilesh Dalvi
 
PPTX
Exception handling c++
Jayant Dalvi
 
PPTX
Type conversion
PreethaPreetha5
 
PPT
Method overriding
Azaz Maverick
 
PPT
Formatted input and output
Online
 
PPTX
Inheritance in c++
Vineeta Garg
 
friend function(c++)
Ritika Sharma
 
Pure virtual function and abstract class
Amit Trivedi
 
Type casting in java
Farooq Baloch
 
OOPS Basics With Example
Thooyavan Venkatachalam
 
classes and objects in C++
HalaiHansaika
 
Inheritance in java
Tech_MX
 
Constructor in java
Madishetty Prathibha
 
Constructor in java
Pavith Gunasekara
 
Static Data Members and Member Functions
MOHIT AGARWAL
 
JAVA AWT
shanmuga rajan
 
Programming in c Arrays
janani thirupathi
 
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Input and output in C++
Nilesh Dalvi
 
Exception handling c++
Jayant Dalvi
 
Type conversion
PreethaPreetha5
 
Method overriding
Azaz Maverick
 
Formatted input and output
Online
 
Inheritance in c++
Vineeta Garg
 
Ad

Viewers also liked (20)

PPTX
Managing console
Shiva Saxena
 
PPT
cpp input & output system basics
gourav kottawar
 
PPT
File in cpp 2016
Dr .Ahmed Tawwab
 
PPT
Console Io Operations
archikabhatia
 
DOCX
C++ file
Mukund Trivedi
 
PPTX
Managing console input and output
gourav kottawar
 
PPT
File handling in C++
Hitesh Kumar
 
PPTX
basics of file handling
pinkpreet_kaur
 
PPT
File handling
Nilesh Dalvi
 
PPTX
Templates presentation
malaybpramanik
 
PPT
14 file handling
APU
 
PPTX
Templates in C++
Tech_MX
 
PPTX
Unit iv
donny101
 
PDF
Adaptive Query Optimization
Anju Garg
 
PPT
File Handling In C++(OOPs))
Papu Kumar
 
PPTX
2CPP17 - File IO
Michael Heron
 
PPT
Qno 3 (a)
Praveen M Jigajinni
 
PDF
Generic programming and concepts that should be in C++
Anton Kolotaev
 
Managing console
Shiva Saxena
 
cpp input & output system basics
gourav kottawar
 
File in cpp 2016
Dr .Ahmed Tawwab
 
Console Io Operations
archikabhatia
 
C++ file
Mukund Trivedi
 
Managing console input and output
gourav kottawar
 
File handling in C++
Hitesh Kumar
 
basics of file handling
pinkpreet_kaur
 
File handling
Nilesh Dalvi
 
Templates presentation
malaybpramanik
 
14 file handling
APU
 
Templates in C++
Tech_MX
 
Unit iv
donny101
 
Adaptive Query Optimization
Anju Garg
 
File Handling In C++(OOPs))
Papu Kumar
 
2CPP17 - File IO
Michael Heron
 
Generic programming and concepts that should be in C++
Anton Kolotaev
 
Ad

Similar to C++ Files and Streams (20)

PPTX
Streams and Files
Munazza-Mah-Jabeen
 
PDF
Data file handling
Prof. Dr. K. Adisesha
 
PPT
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
AzanMehdi
 
PPTX
Filesin c++
HalaiHansaika
 
PPTX
Working with files in c++. file handling
tfluid16
 
PPTX
File management in C++
apoorvaverma33
 
PPT
File handling in_c
sanya6900
 
PPTX
file.pptx 43dcsddsafgdewdvvbghghsdwweffr
abdelhamidatef1
 
PPT
file_handling_in_c.ppt......................................
nadoj47203
 
PPT
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
SanskritiGupta39
 
PPT
csc1201_lecture13.ppt
HEMAHEMS5
 
PPTX
13 file handling in C++.pptx oops object oriented programming
archana22486y
 
PPTX
Chapter4.pptx
WondimuBantihun1
 
PDF
Filesinc 130512002619-phpapp01
Rex Joe
 
PDF
Files in C++.pdf is the notes of cpp for reference
anuvayalil5525
 
PPTX
File handling.pptx
VishuSaini22
 
PPTX
Files in c++
NivethaJeyaraman
 
PPT
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
PPTX
Data file handling in c++
Vineeta Garg
 
PPTX
Introduction to files management systems
araba8
 
Streams and Files
Munazza-Mah-Jabeen
 
Data file handling
Prof. Dr. K. Adisesha
 
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
AzanMehdi
 
Filesin c++
HalaiHansaika
 
Working with files in c++. file handling
tfluid16
 
File management in C++
apoorvaverma33
 
File handling in_c
sanya6900
 
file.pptx 43dcsddsafgdewdvvbghghsdwweffr
abdelhamidatef1
 
file_handling_in_c.ppt......................................
nadoj47203
 
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
SanskritiGupta39
 
csc1201_lecture13.ppt
HEMAHEMS5
 
13 file handling in C++.pptx oops object oriented programming
archana22486y
 
Chapter4.pptx
WondimuBantihun1
 
Filesinc 130512002619-phpapp01
Rex Joe
 
Files in C++.pdf is the notes of cpp for reference
anuvayalil5525
 
File handling.pptx
VishuSaini22
 
Files in c++
NivethaJeyaraman
 
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
Data file handling in c++
Vineeta Garg
 
Introduction to files management systems
araba8
 

More from Ahmed Farag (14)

PPTX
Intro to php
Ahmed Farag
 
PPTX
MYSql manage db
Ahmed Farag
 
PPTX
Normalization
Ahmed Farag
 
PPTX
Sql modifying data - MYSQL part I
Ahmed Farag
 
PPTX
MYSQL using set operators
Ahmed Farag
 
PPTX
MYSQL join
Ahmed Farag
 
PPTX
MYSQL single rowfunc-multirowfunc-groupby-having
Ahmed Farag
 
PPTX
Introduction to NoSQL and MongoDB
Ahmed Farag
 
PPTX
Introduction to OOP concepts
Ahmed Farag
 
PPTX
Introduction to object-oriented analysis and design (OOA/D)
Ahmed Farag
 
PPTX
OOP C++
Ahmed Farag
 
PPTX
Functions C++
Ahmed Farag
 
PPTX
intro to pointer C++
Ahmed Farag
 
PPTX
Intro to C++
Ahmed Farag
 
Intro to php
Ahmed Farag
 
MYSql manage db
Ahmed Farag
 
Normalization
Ahmed Farag
 
Sql modifying data - MYSQL part I
Ahmed Farag
 
MYSQL using set operators
Ahmed Farag
 
MYSQL join
Ahmed Farag
 
MYSQL single rowfunc-multirowfunc-groupby-having
Ahmed Farag
 
Introduction to NoSQL and MongoDB
Ahmed Farag
 
Introduction to OOP concepts
Ahmed Farag
 
Introduction to object-oriented analysis and design (OOA/D)
Ahmed Farag
 
OOP C++
Ahmed Farag
 
Functions C++
Ahmed Farag
 
intro to pointer C++
Ahmed Farag
 
Intro to C++
Ahmed Farag
 

Recently uploaded (20)

PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Virus sequence retrieval from NCBI database
yamunaK13
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 

C++ Files and Streams

  • 1. C++ [ FILES AND STREAMS ] AHMED FARAG INFORMATION SYSTEM DEPARTMENT, FACULTY OF COMPUTER AND INFORMATION, MENUFIA UNIVERSITY
  • 2. FILES AND STREAMS  So far, we have been using the iostream standard library, which provides 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
  • 4. NOTE  To perform file processing in C++, header files <iostream> and <fstream> must be included in your C++ source file.
  • 5. OPENING A FILE  A file must be opened before you can read from it or write to it. Either theofstream 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);
  • 6. OPENING A FILE CON…  The first argument specifies the name and location of the file to be opened and the second argument of the open() member function defines the mode in which the file should be opened.
  • 7. OPENING A FILE CON…  You can combine two or more of these values by ORing them together. For example if you want to open a file in write mode and want to truncate it in case it already exists, following will be the syntax: ofstream outfile; outfile.open("file.dat", ios::out | ios::trunc ); fstream afile; afile.open("file.dat", ios::out | ios::in );
  • 8. CLOSING A FILE  When a C++ program terminates it automatically closes flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination.  standard syntax for close() function void close();
  • 9. WRITING TO A FILE  While doing C++ programming, you write information to a file from your program using the stream insertion operator (<<) just as you use that operator to output information to the screen.The only difference is that you use an ofstream or fstream object instead of the cout object.
  • 10. READING FROM A FILE  You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard.The only difference is that you use an ifstream or fstream object instead of the cin object.
  • 11. READ & WRITE EXAMPLE  https://ideone.com/gir9Ww
  • 12. READ & WRITE EXAMPLE  https://ideone.com/Io5oNw