SlideShare una empresa de Scribd logo
1 de 27
FILE HANDLING IN C++
Presented to:- mrs. sonymol koshy
By:- PaPu kumar
section:- B
roll no.:- 2061424(47)
semester:- iii
CONTENTS OUTLINE
 Data File Handling:
 Need for a data file,
 Types of data files – Text file and Binary file;
 Text File:
 Basic file operations on text file:
 Creating/Writing text into file,
 Reading and
 manipulation of text from an already existing text File (accessing sequentially);
 Binary File:
 Creation of file,
 Writing data into file,
 Searching for required data from file,
 Appending data to a file,
 Insertion of data in sorted file,
 Deletion of data from file,
 Modification of data in a file;
Implementation of above mentioned data file handling in C++; Components of C++ to be
used with file handling:
CONTENTS OUTLINE
 Header file:
 fstream.h; ifstream, ofstream, fstream classes;
 Opening a text file in in, out, and app modes;
 open(),
 get(),
 put(),
 getline() and
 close() functions;
 Detecting end-of-file (with or without using eof() function);
 Opening a binary file using in, out, and app modes;
 open(), read(), write() and close() functions;
 All programs we looked earlier:
Introduction
 input data from the keyboard.
 output data to the screen.
 Difficult to handle large amount of input data.
 Output would also be lost as soon as we exit
from the program.
 How do we store data permanently?.
 We can use secondary storage device.
 Data is packaged up on the storage device as data
structures called files.
Files (Streams)
 Files are used to store data in a relatively permanent form, on floppy
disk, hard disk, tape or other form of secondary storage. Files can hold
huge amounts of data if need be. Ordinary variables (even records and
arrays) are kept in main memory which is temporary and rather limited in
size.
Lets put it in points…………..
Why use files?
• Convenient way to deal with large quantities of data.
• Store data permanently (until file is deleted).
• Avoid having to type data into program multiple times.
• Share data between programs.
The following is a
comparison
of
the two types of storage………..
Main memory
• Made up of RAM chips.
• Used to hold a program
when it is running,
including the values of its
variables (whether
integer, char, an array,
etc.)
Secondary memory
• Usually a disk drive (or
magnetic tape).
• Used to hold files (where a file
can contain data, a program,
text, etc.)
• Can hold rather large amounts
of data.
Main memory
• Can only hold relatively small amounts
of data.
• Is temporary (as soon as the program is
done or the power goes out all of these
values are gone).
• Gives fast access to the data (all
electronic).
Secondary memory
• Can hold rather large amounts of data.
• Is fairly permanent. (A file remains even if
the power goes out. It will last until you
erase it, as long as the disk isn't damaged,
at least.)
 Access to the data is considerably slower
(due to moving parts).
I/O in C++
 I/O in C++ uses streams
 A Stream is a general name given to
flow of data.
Flow of Data….
PROGRAM
DEVICES OR
FILES
Input
Stream
>>
Output
Stream
<<
Data
Data
istream class ostream class
(Insertion
operator)
(Extraction
operator)
More About Files…..
 Now we need to know:
 how to "connect" file to program
 how to tell the program to read data
 how to tell the program to write data
 error checking and handling eof
I/O in C++
 Different streams are used to represent different kinds
of data flow.
 Each stream is associated with a particular class, which
contains
 member functions and
 definitions for dealing with that particular kind of data
flow.
The following classes in C++ have
access to file input and output
functions:
 ifstream
 ofstream
 fstream
File Related Classes
The Stream Class Hierarchy
ios
istream
get()
getline()
read()
>>
ostream
put()
write()
<<
fstreambase
iostream
Ifstream
Open()
Tellg()
Seekg()
Ofstream
Open()
Tellp()
Seekp()
fstream
NOTE : UPWARD ARROWS INDICATE
THE BASE CLASS
OPENING A FILE
1. By using the CONSTRUCTOR of the stream class.
ifstream transaction(“sales.dly”);
ofstream result(“result.02”);
2. By using the open() function of the stream class
ifstream transaction;
transaction.open(“sales.dly”);
(Associating a stream with a file)
File Mode Parameters
PARAMETER MEANING
 ios::app Append to end-of file
 ios::ate goto end of file on opening
 ios::binary binary file
 ios::in Open existing file for reading
 ios::nocreate open fails if file doesn’t exist
 ios::noreplace open fails if file already exists
 ios::out creates new file for writing on
 ios::trunc Deletes contents if it exists
The mode can combine two or more modes using bit wise or ( | )
Checking For Successful File Opening
ifstream transaction(“sales.dly”);
if (transaction == NULL)
{
cout<<“unable to open sales.dly”;
cin.get(); // waits for the operator to press any key
exit(1);
}
Closing of File
Stream_name.close();
e.g., transaction.close();
fl.close();
Note : There is no need to give the physical
file name at the time of closing a file.
Types of Files
 The two basic types of files are
 Text files
&
 Binary files
Text Files
 A text file consists of readable characters separated into lines by
newline characters.
 (On most PCs, the newline character is actually represented by the two-
character sequence of carriage return (ASCII 13), line feed (ASCII 10).
(n)
A binary file stores data to disk in the
same form in which it is represented in
main memory.
 If you ever try to edit a binary file
containing numbers you will see that the
numbers appear as nonsense characters.
Binary Files
Not having to translate numbers into a
readable form makes binary files
somewhat more efficient.
Binary files also do not normally use
anything to separate the data into
lines.
Such a file is just a stream of data
with nothing in particular to separate
components.
Binary Files
 When using a
binary file we
write whole record
data to the file at
once.
 but the numbers in
the binary file will
not be readable in
this way.
 When using a text
file, we write out
separately each of
the pieces of data
about a given
record.
 The text file will be
readable by an
editor
Text Files Binary Files
 for the text file we will
use the usual output
operator(<<) and will
output each of the
pieces of the record
separately.
 with the text file we
will read each of the
pieces of record from
the file separately,
using the usual input
operator(>>)
 For the binary file we
will use write to write to
the file,
 With the binary file we
will use the read
function to read a
whole record,
The programs to create the data files will differ in how they
open the file and in how they write to the file.
:Sequential access. With this type of
file access one must read the data in
order, much like with a tape, whether
the data is really stored on tape or not.
Random access (or direct access). This
type of file access lets you jump to any
location in the file, then to any other,
etc., all in a reasonable amount of time.
Types of File Access
The End

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Exception handling
Exception handlingException handling
Exception handling
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
Friend function
Friend functionFriend function
Friend function
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Inheritance C#
Inheritance C#Inheritance C#
Inheritance C#
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Friend Function
Friend FunctionFriend Function
Friend Function
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
08. handling file streams
08. handling file streams08. handling file streams
08. handling file streams
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
class and objects
class and objectsclass and objects
class and objects
 
Python functions
Python functionsPython functions
Python functions
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
 

Similar a C++ File Handling Guide

Similar a C++ File Handling Guide (20)

Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9
 
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_cFile handling in_c
File handling in_c
 
Data file handling
Data file handlingData file handling
Data file handling
 
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
 
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
 
File Management and manipulation in C++ Programming
File Management and manipulation in C++ ProgrammingFile Management and manipulation in C++ Programming
File Management and manipulation in C++ Programming
 
File Handling
File HandlingFile Handling
File Handling
 
File management in C++
File management in C++File management in C++
File management in C++
 
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
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
File Handling
File HandlingFile Handling
File Handling
 
File Handling
File HandlingFile Handling
File Handling
 
Python-FileHandling.pptx
Python-FileHandling.pptxPython-FileHandling.pptx
Python-FileHandling.pptx
 
Deletion of a Record from a File - K Karun
Deletion of a Record from a File - K KarunDeletion of a Record from a File - K Karun
Deletion of a Record from a File - K Karun
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 
Data file handling
Data file handlingData file handling
Data file handling
 

Último

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 

Último (20)

Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
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🔝
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 

C++ File Handling Guide

  • 1. FILE HANDLING IN C++ Presented to:- mrs. sonymol koshy By:- PaPu kumar section:- B roll no.:- 2061424(47) semester:- iii
  • 2. CONTENTS OUTLINE  Data File Handling:  Need for a data file,  Types of data files – Text file and Binary file;  Text File:  Basic file operations on text file:  Creating/Writing text into file,  Reading and  manipulation of text from an already existing text File (accessing sequentially);  Binary File:  Creation of file,  Writing data into file,  Searching for required data from file,  Appending data to a file,  Insertion of data in sorted file,  Deletion of data from file,  Modification of data in a file; Implementation of above mentioned data file handling in C++; Components of C++ to be used with file handling:
  • 3. CONTENTS OUTLINE  Header file:  fstream.h; ifstream, ofstream, fstream classes;  Opening a text file in in, out, and app modes;  open(),  get(),  put(),  getline() and  close() functions;  Detecting end-of-file (with or without using eof() function);  Opening a binary file using in, out, and app modes;  open(), read(), write() and close() functions;
  • 4.  All programs we looked earlier: Introduction  input data from the keyboard.  output data to the screen.  Difficult to handle large amount of input data.  Output would also be lost as soon as we exit from the program.  How do we store data permanently?.  We can use secondary storage device.  Data is packaged up on the storage device as data structures called files.
  • 5. Files (Streams)  Files are used to store data in a relatively permanent form, on floppy disk, hard disk, tape or other form of secondary storage. Files can hold huge amounts of data if need be. Ordinary variables (even records and arrays) are kept in main memory which is temporary and rather limited in size. Lets put it in points…………..
  • 6. Why use files? • Convenient way to deal with large quantities of data. • Store data permanently (until file is deleted). • Avoid having to type data into program multiple times. • Share data between programs.
  • 7. The following is a comparison of the two types of storage………..
  • 8. Main memory • Made up of RAM chips. • Used to hold a program when it is running, including the values of its variables (whether integer, char, an array, etc.) Secondary memory • Usually a disk drive (or magnetic tape). • Used to hold files (where a file can contain data, a program, text, etc.) • Can hold rather large amounts of data.
  • 9. Main memory • Can only hold relatively small amounts of data. • Is temporary (as soon as the program is done or the power goes out all of these values are gone). • Gives fast access to the data (all electronic). Secondary memory • Can hold rather large amounts of data. • Is fairly permanent. (A file remains even if the power goes out. It will last until you erase it, as long as the disk isn't damaged, at least.)  Access to the data is considerably slower (due to moving parts).
  • 10. I/O in C++  I/O in C++ uses streams  A Stream is a general name given to flow of data.
  • 11. Flow of Data…. PROGRAM DEVICES OR FILES Input Stream >> Output Stream << Data Data istream class ostream class (Insertion operator) (Extraction operator)
  • 12. More About Files…..  Now we need to know:  how to "connect" file to program  how to tell the program to read data  how to tell the program to write data  error checking and handling eof
  • 13. I/O in C++  Different streams are used to represent different kinds of data flow.  Each stream is associated with a particular class, which contains  member functions and  definitions for dealing with that particular kind of data flow.
  • 14. The following classes in C++ have access to file input and output functions:  ifstream  ofstream  fstream File Related Classes
  • 15. The Stream Class Hierarchy ios istream get() getline() read() >> ostream put() write() << fstreambase iostream Ifstream Open() Tellg() Seekg() Ofstream Open() Tellp() Seekp() fstream NOTE : UPWARD ARROWS INDICATE THE BASE CLASS
  • 16. OPENING A FILE 1. By using the CONSTRUCTOR of the stream class. ifstream transaction(“sales.dly”); ofstream result(“result.02”); 2. By using the open() function of the stream class ifstream transaction; transaction.open(“sales.dly”); (Associating a stream with a file)
  • 17. File Mode Parameters PARAMETER MEANING  ios::app Append to end-of file  ios::ate goto end of file on opening  ios::binary binary file  ios::in Open existing file for reading  ios::nocreate open fails if file doesn’t exist  ios::noreplace open fails if file already exists  ios::out creates new file for writing on  ios::trunc Deletes contents if it exists The mode can combine two or more modes using bit wise or ( | )
  • 18. Checking For Successful File Opening ifstream transaction(“sales.dly”); if (transaction == NULL) { cout<<“unable to open sales.dly”; cin.get(); // waits for the operator to press any key exit(1); }
  • 19. Closing of File Stream_name.close(); e.g., transaction.close(); fl.close(); Note : There is no need to give the physical file name at the time of closing a file.
  • 20. Types of Files  The two basic types of files are  Text files &  Binary files
  • 21. Text Files  A text file consists of readable characters separated into lines by newline characters.  (On most PCs, the newline character is actually represented by the two- character sequence of carriage return (ASCII 13), line feed (ASCII 10). (n)
  • 22. A binary file stores data to disk in the same form in which it is represented in main memory.  If you ever try to edit a binary file containing numbers you will see that the numbers appear as nonsense characters. Binary Files
  • 23. Not having to translate numbers into a readable form makes binary files somewhat more efficient. Binary files also do not normally use anything to separate the data into lines. Such a file is just a stream of data with nothing in particular to separate components. Binary Files
  • 24.  When using a binary file we write whole record data to the file at once.  but the numbers in the binary file will not be readable in this way.  When using a text file, we write out separately each of the pieces of data about a given record.  The text file will be readable by an editor Text Files Binary Files
  • 25.  for the text file we will use the usual output operator(<<) and will output each of the pieces of the record separately.  with the text file we will read each of the pieces of record from the file separately, using the usual input operator(>>)  For the binary file we will use write to write to the file,  With the binary file we will use the read function to read a whole record, The programs to create the data files will differ in how they open the file and in how they write to the file.
  • 26. :Sequential access. With this type of file access one must read the data in order, much like with a tape, whether the data is really stored on tape or not. Random access (or direct access). This type of file access lets you jump to any location in the file, then to any other, etc., all in a reasonable amount of time. Types of File Access