SlideShare a Scribd company logo
1 of 24
Download to read offline
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 1
Haresh Jaiswal
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 2
Introduction
 Computers have 2 types of memory.
 Primary memory (temporary storage)
 Secondary memory (permanent storage)
 All programs which we have studied earlier
 Inputs data from keyboard
 Outputs results to the screen
 We have utilised computers primary memory to store data, in the
form of variables, arrays, structure/class instances.
 After the termination of program all the entered data is lost as it
is stored on primary memory.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 3
How to store data permanently?
 Most real-time application needs to store data permanently, so
that it can be used later, then it becomes necessary to keep it in
permanent storage device.
 To store the data in permanent form
 We have to store the data on secondary storage.
 On secondary storage data is stored in the form of files.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 4
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.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 5
Streams
 A c++ program can read or write data from/to a disk file.
 Streams are used to communicate with disk files.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 6
What are Streams?
 Streams are mediator between a C++ program & various
resources of computer, such as
 keyboard,
 monitor,
 disk files.
 Streams are of 2 types
 Input stream
 Output stream
 A Stream is internally nothing but a series of characters.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 7
What are Streams?
 A stream is an abstraction that represents a device on which
input and output operations were performed. A stream can
basically be represented as a source or destination of characters
of indefinite length.
 Streams are generally associated to a physical source or
destination of characters,
 like a disk file,
 the keyboard,
 or the console,
 So the characters gotten or written to/from streams are
physically input/output to the physical device.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 8
What are Streams?
 We have already studied 2 standard input/output streams
 cout
 cin
 cout is an output stream, which outputs data to the monitor.
 cin is an input stream, which inputs data from keyboard.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 9
What are Streams?
 We can define other streams as well (file streams)
 Which redirects data to or from disk files
 Can be used the same way as we have used cout & cin
 Streams work with built-in data types, and you can make user-
defined types work with streams by overloading the insertion
operator (<<) to put objects into streams, and the extraction
operator (>>) to read objects from streams.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 10
File Streams
 Programs can be designed to perform the read and write
operations on these files.
 File Streams are used to communicate with disk files.
Disk
Files
C++
Program
Read Data
from disk file
Input data for
processing
Write Data to
disk file
Output File Stream
Input File Stream
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 11
File Streams
 File Streams are of 2 types
 Input file stream
 Output file stream
Disk
Files
C++
Program
Read Data
from disk file
Input data for
processing
Write Data to
disk file
Output File Stream
Input File Stream
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 12
Input File Streams
 Input file streams are used to input data from a disk file.
 Input file streams supplies data from disk file to the program.
Disk
Files
C++
Program
Read Data
from disk file
Input data for
processing
Write Data to
disk file
Output File Stream
Input File Stream
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 13
Output File Streams
 Output file streams sends/writes data to the disk file.
Disk
Files
C++
Program
Read Data
from disk file
Input data for
processing
Write Data to
disk file
Output File Stream
Input File Stream
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 14
Input/output with Files
 C++ provides the following classes to perform output and input of
characters to/from disk files:
 ofstream : Stream class to write to files
 ifstream : Stream class to read from files
 fstream : Stream class to both read and write from/to files.
 To use these classes, we must include the <fstream> header file.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 15
Input/output with Files
 In C++, a stream is represented by an object of a particular
stream class.
 The stream class hierarchy is as follows
ios
istream ostream
ifstream ofstream
iostream
fstream
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 16
Input/output with Files
 A Stream variable must be declared like any other class variable,
for example,
ifstream myInputStream;
ofstream myOutputStream;
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 17
Input/output with Files
 Once stream object has been declared, we must connect it to a
physical disk file, In order to open a file with a stream object we
use its member function open().
StreamObject.open (FileName, [Mode]);
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 18
Input/output with Files
StreamObject.open (FileName, [Mode]);
 Open method has 2 parameters
FileName : name/full path of physical disk file to be opened
Mode : purpose for opening a file, (Reading/Writing)
 The mode parameter is optional for ifstream & ofstream, as
ifstream implicitly means input mode, and ofstream implicitly
means output mode, for a fstream object both parameters are
necessary.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 19
Various Modes for Opening File
 Mode is an optional parameter with a combination of the
following flags:
ios :: in Open file for input operations.
ios :: out Open file for output operations.
ios :: binary Open in binary mode.
ios :: ate
Set the initial position at the end of the file.
If this flag is not set, the initial position is the beginning of the file.
ios :: app
All output operations are performed at the end of the file, appending the content to
the current content of the file.
ios :: trunc
If the file is opened for output operations and it already existed, its previous content
is deleted and replaced by the new one.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 20
Writing data to Files (file output)
 Once declared we can use file streams normally in the same
fashion we have used cout and cin streams.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 21
Writing data to Files (file output)
 This code creates a file called TestFile.txt and inserts a sentence into it, the
same way we are used to do with cout, but using the file
stream MyOutputStream instead.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 22
Writing data to Files (file output)
 Filename & mode can be provided to the constructor of stream
class, instead of calling open() method explicitly.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 23
Writing data to Files (file output)
 As we know mode is optional for ofstream and ifstream, so its not
necessary to specify it. Specify only file name to be open
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 24
Reading data from Files (file input)
 Data can be retrieved from a file input stream the same way we
read from standard input stream ‘cin’.
Data Retrieved : Hello
Output

More Related Content

What's hot

What's hot (20)

Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Assemblers: Ch03
Assemblers: Ch03Assemblers: Ch03
Assemblers: Ch03
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Console i/o for c++
Console i/o for c++Console i/o for c++
Console i/o for c++
 
File handling in c
File handling in cFile handling in c
File handling in c
 
File operations in c
File operations in cFile operations in c
File operations in c
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Computer registers
Computer registersComputer registers
Computer registers
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C Language
 
C language unit-1
C language unit-1C language unit-1
C language unit-1
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
Data structure
Data structureData structure
Data structure
 
The string class
The string classThe string class
The string class
 
Assembler - System Programming
Assembler - System ProgrammingAssembler - System Programming
Assembler - System Programming
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
 
C tokens
C tokensC tokens
C tokens
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compiler
 

Similar to 08. handling file streams

Similar to 08. handling file streams (20)

File handling in_c
File handling in_cFile handling in_c
File handling in_c
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
 
Basics of files and its functions with example
Basics of files and its functions with exampleBasics of files and its functions with example
Basics of files and its functions with example
 
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
 
File management in C++
File management in C++File management in C++
File management in C++
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
Cs 1114 - lecture-29
Cs 1114 - lecture-29Cs 1114 - lecture-29
Cs 1114 - lecture-29
 
Lec 49 - stream-files
Lec 49 - stream-filesLec 49 - stream-files
Lec 49 - stream-files
 
cpp-file-handling
cpp-file-handlingcpp-file-handling
cpp-file-handling
 
Cpp file-handling
Cpp file-handlingCpp file-handling
Cpp file-handling
 
Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)
 
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
 
File Handling in C++
File Handling in C++File Handling in C++
File Handling in C++
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
file management functions
file management functionsfile management functions
file management functions
 
File management
File managementFile management
File management
 

More from Haresh Jaiswal

06. operator overloading
06. operator overloading06. operator overloading
06. operator overloadingHaresh Jaiswal
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructorHaresh Jaiswal
 
02. functions & introduction to class
02. functions & introduction to class02. functions & introduction to class
02. functions & introduction to classHaresh Jaiswal
 
01. introduction to C++
01. introduction to C++01. introduction to C++
01. introduction to C++Haresh Jaiswal
 

More from Haresh Jaiswal (7)

06. operator overloading
06. operator overloading06. operator overloading
06. operator overloading
 
05. inheritance
05. inheritance05. inheritance
05. inheritance
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructor
 
03. oop concepts
03. oop concepts03. oop concepts
03. oop concepts
 
02. functions & introduction to class
02. functions & introduction to class02. functions & introduction to class
02. functions & introduction to class
 
01. introduction to C++
01. introduction to C++01. introduction to C++
01. introduction to C++
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 

Recently uploaded

AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 

Recently uploaded (20)

AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 

08. handling file streams

  • 1. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 1 Haresh Jaiswal
  • 2. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 2 Introduction  Computers have 2 types of memory.  Primary memory (temporary storage)  Secondary memory (permanent storage)  All programs which we have studied earlier  Inputs data from keyboard  Outputs results to the screen  We have utilised computers primary memory to store data, in the form of variables, arrays, structure/class instances.  After the termination of program all the entered data is lost as it is stored on primary memory.
  • 3. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 3 How to store data permanently?  Most real-time application needs to store data permanently, so that it can be used later, then it becomes necessary to keep it in permanent storage device.  To store the data in permanent form  We have to store the data on secondary storage.  On secondary storage data is stored in the form of files.
  • 4. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 4 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.
  • 5. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 5 Streams  A c++ program can read or write data from/to a disk file.  Streams are used to communicate with disk files.
  • 6. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 6 What are Streams?  Streams are mediator between a C++ program & various resources of computer, such as  keyboard,  monitor,  disk files.  Streams are of 2 types  Input stream  Output stream  A Stream is internally nothing but a series of characters.
  • 7. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 7 What are Streams?  A stream is an abstraction that represents a device on which input and output operations were performed. A stream can basically be represented as a source or destination of characters of indefinite length.  Streams are generally associated to a physical source or destination of characters,  like a disk file,  the keyboard,  or the console,  So the characters gotten or written to/from streams are physically input/output to the physical device.
  • 8. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 8 What are Streams?  We have already studied 2 standard input/output streams  cout  cin  cout is an output stream, which outputs data to the monitor.  cin is an input stream, which inputs data from keyboard.
  • 9. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 9 What are Streams?  We can define other streams as well (file streams)  Which redirects data to or from disk files  Can be used the same way as we have used cout & cin  Streams work with built-in data types, and you can make user- defined types work with streams by overloading the insertion operator (<<) to put objects into streams, and the extraction operator (>>) to read objects from streams.
  • 10. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 10 File Streams  Programs can be designed to perform the read and write operations on these files.  File Streams are used to communicate with disk files. Disk Files C++ Program Read Data from disk file Input data for processing Write Data to disk file Output File Stream Input File Stream
  • 11. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 11 File Streams  File Streams are of 2 types  Input file stream  Output file stream Disk Files C++ Program Read Data from disk file Input data for processing Write Data to disk file Output File Stream Input File Stream
  • 12. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 12 Input File Streams  Input file streams are used to input data from a disk file.  Input file streams supplies data from disk file to the program. Disk Files C++ Program Read Data from disk file Input data for processing Write Data to disk file Output File Stream Input File Stream
  • 13. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 13 Output File Streams  Output file streams sends/writes data to the disk file. Disk Files C++ Program Read Data from disk file Input data for processing Write Data to disk file Output File Stream Input File Stream
  • 14. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 14 Input/output with Files  C++ provides the following classes to perform output and input of characters to/from disk files:  ofstream : Stream class to write to files  ifstream : Stream class to read from files  fstream : Stream class to both read and write from/to files.  To use these classes, we must include the <fstream> header file.
  • 15. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 15 Input/output with Files  In C++, a stream is represented by an object of a particular stream class.  The stream class hierarchy is as follows ios istream ostream ifstream ofstream iostream fstream
  • 16. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 16 Input/output with Files  A Stream variable must be declared like any other class variable, for example, ifstream myInputStream; ofstream myOutputStream;
  • 17. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 17 Input/output with Files  Once stream object has been declared, we must connect it to a physical disk file, In order to open a file with a stream object we use its member function open(). StreamObject.open (FileName, [Mode]);
  • 18. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 18 Input/output with Files StreamObject.open (FileName, [Mode]);  Open method has 2 parameters FileName : name/full path of physical disk file to be opened Mode : purpose for opening a file, (Reading/Writing)  The mode parameter is optional for ifstream & ofstream, as ifstream implicitly means input mode, and ofstream implicitly means output mode, for a fstream object both parameters are necessary.
  • 19. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 19 Various Modes for Opening File  Mode is an optional parameter with a combination of the following flags: ios :: in Open file for input operations. ios :: out Open file for output operations. ios :: binary Open in binary mode. ios :: ate Set the initial position at the end of the file. If this flag is not set, the initial position is the beginning of the file. ios :: app All output operations are performed at the end of the file, appending the content to the current content of the file. ios :: trunc If the file is opened for output operations and it already existed, its previous content is deleted and replaced by the new one.
  • 20. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 20 Writing data to Files (file output)  Once declared we can use file streams normally in the same fashion we have used cout and cin streams.
  • 21. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 21 Writing data to Files (file output)  This code creates a file called TestFile.txt and inserts a sentence into it, the same way we are used to do with cout, but using the file stream MyOutputStream instead.
  • 22. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 22 Writing data to Files (file output)  Filename & mode can be provided to the constructor of stream class, instead of calling open() method explicitly.
  • 23. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 23 Writing data to Files (file output)  As we know mode is optional for ofstream and ifstream, so its not necessary to specify it. Specify only file name to be open
  • 24. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 24 Reading data from Files (file input)  Data can be retrieved from a file input stream the same way we read from standard input stream ‘cin’. Data Retrieved : Hello Output