SlideShare a Scribd company logo
1 of 24
Swami Keshvanand Institute of Technology, Management & Gramothan (SKIT)
Name: Tushar Batra
Branch: Computer Science
Topic: file Handling in C++
Semester: 3rd
Section: E
Roll No: 19ESKCS853
Teacher: Kailash Soni
FILE HANDLING
IN C++
INTRODUCTION
 Computer programs are associated to work with files as it
helps in storing data & information permanently.
 File - itself a bunch of bytes stored on some storage
devices.
 In C++ this is achieved through a component header file
called fstream.h
 The I/O library manages two aspects- as interface and for
transfer of data.
 The library predefine a set of operations for all file related
handling through certain classes.
THE FSTREAM.H HEADER FILEA stream is a general term used to name flow of data.
Streams act as an interface between files and programs.
A Stream is sequence of bytes.
They represent as a sequence of bytes and deals with the
flow of data.
Every stream is associated with a class having member
functions and operations for a particular kind of data flow.
File  Program ( Input stream) - reads
Program  File (Output stream) – write
All designed into fstream.h and hence needs to be included
in all file handling programs.
Diagrammatically as shown in next slide
FUNCTIONS OF FILE STREAM CLASSES
 filebuf – It sets the buffer to read and write, it contains
close() and open() member functions on it.
 fstreambase – this is the base class for fstream and,
ifstream and ofstream classes. therefore it provides the
common function to these classes. It also contains open()
and close() functions.
 ifstream – Being input class it provides input operations it
inherits the functions get( ), getline( ), read( ), and random
access functions seekg( ) and tellg( ) functions.
 ofstream – Being output class it provides output
operations it inherits put( ), write( ) and random access
functions seekp( ) and tellp( ) functions.
 fstream – it is an i/o class stream, it provides
simultaneous input and output operations.
FILE TYPES
A File can be stored in two ways
Text File
Binary File
Text Files : Stores information in ASCII characters. In text file
each line of text is terminated by with special character known
as EOL (End of Line) In text file some translations takes place
when this EOL character is read or written.
Binary File: it contains the information in the same format as
it is held in the memory. In binary file there is no delimiter for a
line. Also no translation occur in binary file. As a result binary
files are faster and easier for program to read and write.
FILE MODES
WHAT IS FILE MODE?
The File Mode describes how a file is to be used ; to
read from it, write to it, to append and so on
Syntax
Stream_object.open(“filename”,mode);
File Modes
ios::out: It open file in output mode (i.e write mode) and
place the file pointer in beginning, if file already exist it will
overwrite the file.
ios::in It open file in input mode(read mode) and permit
reading from the file.
FILE MODES
ios::app It open the file in write mode, and place file pointer
at the end of file i.e to add new contents and retains previous
contents. If file does not exist it will create a new file.
ios::ate It open the file in write or read mode, and place file
pointer at the end of file i.e input/ output operations can
performed anywhere in the file.
ios::trunc It truncates the existing file (empties the file).
ios::nocreate If file does not exist this file mode ensures that
no file is created and open() fails.
 ios::noreplace If file does not exist, a new file gets created
but if the file already exists, the open() fails.
ios::binary Opens a file in binary mode.
Closing a File
 A File is closed by disconnecting it with the stream it is
associated with. The close( ) function is used to
accomplish this task.
Syntax:
Stream_object.close( );
Example :
fout.close();
Steps To Create A File
1. Declare an object of the desired file stream class(ifstream,
ofstream, or fstream)
2. Open the required file to be processed using constructor or
open function.
3. Process the file.
4. Close the file stream using the object of file stream.
eof ( ) Function
This function determines the end-of-file by returning true(non-
zero) for end of file otherwise returning false(zero).
Syntax
Stream_object.eof( );
Example :
fout.eof( );
Text File Functions
get() – read a single character from text file and store in a
buffer.
e.g file.get(ch);
put() - writing a single character in textfile
e.g. file.put(ch);
getline() - read a line of text from text file store in a buffer.
e.g file.getline(s,80);
We can also use file>>ch for reading and file<<ch writing
in text file. But >> operator does not accept white spaces.
Program to create a text file using strings I/O
Created File
Program to read content of file and display it
Program to edit the content of file and display it
Updated File
Binary File Functions
read( )- read a block of binary data or reads a fixed number of
bytes from the specified stream and store in a buffer.
Syntax : Stream_object.read((char *)& Object, sizeof(Object));
e.g file.read((char *)&s, sizeof(s));
write( ) – write a block of binary data or writes fixed number of
bytes from a specific memory location to the specified stream.
Syntax : Stream_object.write((char *)& Object, sizeof(Object));
e.g file.write((char *)&s, sizeof(s)); 19
Binary File Functions
Note: Both functions take two arguments.
• The first is the address of variable, and the second is the
length of that variable in bytes. The address of variable must
be type cast to type char*(pointer to character type)
• The data written to a file using write( ) can only be read
accurately using read( ).
20
Program to create a binary file
File Pointer
The file pointer indicates the position in the file at which the
next input/output is to occur.
Moving the file pointer in a file for various operations viz
modification, deletion , searching etc. Following functions are
used
seekg(): It places the file pointer to the specified position in
input mode of file.
e.g file.seekg(p,ios::beg); or
file.seekg(-p,ios::end), or
file.seekg(p,ios::cur)
i.e to move to p byte position from beginning, end or current
position. 23
File Pointer
seekp(): It places the file pointer to the specified position in
output mode of file.
e.g file.seekp(p,ios::beg); or file.seekp(-p,ios::end), or
file.seekp(p,ios::cur)
i.e to move to p byte position from beginning, end or current
position.
tellg(): This function returns the current working position of the
file pointer in the input mode.
e.g int p=file.tellg();
tellp(): This function returns the current working position of the
file pointer in the output mode.
e.f int p=file.tellp();
24

More Related Content

What's hot

File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))Papu Kumar
 
Data file handling in python reading & writing methods
Data file handling in python reading & writing methodsData file handling in python reading & writing methods
Data file handling in python reading & writing methodskeeeerty
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++Shyam Gupta
 
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 KarunDipayan Sarkar
 
08. handling file streams
08. handling file streams08. handling file streams
08. handling file streamsHaresh Jaiswal
 
Chapter28 data-file-handling
Chapter28 data-file-handlingChapter28 data-file-handling
Chapter28 data-file-handlingDeepak Singh
 
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 SYLLABUSVenugopalavarma Raja
 
File handling in_c
File handling in_cFile handling in_c
File handling in_csanya6900
 
File handling in C++
File handling in C++File handling in C++
File handling in C++Hitesh Kumar
 
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)Abdullah khawar
 
Data file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing filesData file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing fileskeeeerty
 

What's hot (19)

File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 
Data file handling in python reading & writing methods
Data file handling in python reading & writing methodsData file handling in python reading & writing methods
Data file handling in python reading & writing methods
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
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
 
08. handling file streams
08. handling file streams08. handling file streams
08. handling file streams
 
Chapter28 data-file-handling
Chapter28 data-file-handlingChapter28 data-file-handling
Chapter28 data-file-handling
 
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 In C++
File Handling In C++File Handling In C++
File Handling In C++
 
Python file handling
Python file handlingPython file handling
Python file handling
 
Files in c++
Files in c++Files in c++
Files in c++
 
data file handling
data file handlingdata file handling
data file handling
 
Cpp file-handling
Cpp file-handlingCpp file-handling
Cpp file-handling
 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
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)
 
Data file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing filesData file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing files
 

Similar to File Handling

File management in C++
File management in C++File management in C++
File management in C++apoorvaverma33
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handlingpinkpreet_kaur
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handlingpinkpreet_kaur
 
chapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfchapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfstudy material
 
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 referenceanuvayalil5525
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Rex Joe
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5YOGESH SINGH
 
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++ ProgrammingChereLemma2
 
INput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptxINput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptxAssadLeo1
 
File handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptxFile handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptxarmaansohail9356
 
DFH PDF-converted.pptx
DFH PDF-converted.pptxDFH PDF-converted.pptx
DFH PDF-converted.pptxAmitKaur17
 
Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4Prerna Sharma
 

Similar to File Handling (20)

Data file handling
Data file handlingData file handling
Data file handling
 
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
 
chapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfchapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdf
 
Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9
 
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
 
Filehandling
FilehandlingFilehandling
Filehandling
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
 
File handling
File handlingFile handling
File handling
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
File Organization
File OrganizationFile Organization
File Organization
 
working with files
working with filesworking with files
working with files
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
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.pptx
FILE HANDLING.pptxFILE HANDLING.pptx
FILE HANDLING.pptx
 
INput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptxINput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptx
 
File handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptxFile handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptx
 
DFH PDF-converted.pptx
DFH PDF-converted.pptxDFH PDF-converted.pptx
DFH PDF-converted.pptx
 
Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4
 

Recently uploaded

Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 

Recently uploaded (20)

Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 

File Handling

  • 1. Swami Keshvanand Institute of Technology, Management & Gramothan (SKIT) Name: Tushar Batra Branch: Computer Science Topic: file Handling in C++ Semester: 3rd Section: E Roll No: 19ESKCS853 Teacher: Kailash Soni
  • 3. INTRODUCTION  Computer programs are associated to work with files as it helps in storing data & information permanently.  File - itself a bunch of bytes stored on some storage devices.  In C++ this is achieved through a component header file called fstream.h  The I/O library manages two aspects- as interface and for transfer of data.  The library predefine a set of operations for all file related handling through certain classes.
  • 4. THE FSTREAM.H HEADER FILEA stream is a general term used to name flow of data. Streams act as an interface between files and programs. A Stream is sequence of bytes. They represent as a sequence of bytes and deals with the flow of data. Every stream is associated with a class having member functions and operations for a particular kind of data flow. File  Program ( Input stream) - reads Program  File (Output stream) – write All designed into fstream.h and hence needs to be included in all file handling programs. Diagrammatically as shown in next slide
  • 5.
  • 6. FUNCTIONS OF FILE STREAM CLASSES  filebuf – It sets the buffer to read and write, it contains close() and open() member functions on it.  fstreambase – this is the base class for fstream and, ifstream and ofstream classes. therefore it provides the common function to these classes. It also contains open() and close() functions.  ifstream – Being input class it provides input operations it inherits the functions get( ), getline( ), read( ), and random access functions seekg( ) and tellg( ) functions.  ofstream – Being output class it provides output operations it inherits put( ), write( ) and random access functions seekp( ) and tellp( ) functions.  fstream – it is an i/o class stream, it provides simultaneous input and output operations.
  • 7. FILE TYPES A File can be stored in two ways Text File Binary File Text Files : Stores information in ASCII characters. In text file each line of text is terminated by with special character known as EOL (End of Line) In text file some translations takes place when this EOL character is read or written. Binary File: it contains the information in the same format as it is held in the memory. In binary file there is no delimiter for a line. Also no translation occur in binary file. As a result binary files are faster and easier for program to read and write.
  • 8. FILE MODES WHAT IS FILE MODE? The File Mode describes how a file is to be used ; to read from it, write to it, to append and so on Syntax Stream_object.open(“filename”,mode); File Modes ios::out: It open file in output mode (i.e write mode) and place the file pointer in beginning, if file already exist it will overwrite the file. ios::in It open file in input mode(read mode) and permit reading from the file.
  • 9. FILE MODES ios::app It open the file in write mode, and place file pointer at the end of file i.e to add new contents and retains previous contents. If file does not exist it will create a new file. ios::ate It open the file in write or read mode, and place file pointer at the end of file i.e input/ output operations can performed anywhere in the file. ios::trunc It truncates the existing file (empties the file). ios::nocreate If file does not exist this file mode ensures that no file is created and open() fails.  ios::noreplace If file does not exist, a new file gets created but if the file already exists, the open() fails. ios::binary Opens a file in binary mode.
  • 10. Closing a File  A File is closed by disconnecting it with the stream it is associated with. The close( ) function is used to accomplish this task. Syntax: Stream_object.close( ); Example : fout.close();
  • 11. Steps To Create A File 1. Declare an object of the desired file stream class(ifstream, ofstream, or fstream) 2. Open the required file to be processed using constructor or open function. 3. Process the file. 4. Close the file stream using the object of file stream.
  • 12. eof ( ) Function This function determines the end-of-file by returning true(non- zero) for end of file otherwise returning false(zero). Syntax Stream_object.eof( ); Example : fout.eof( );
  • 13. Text File Functions get() – read a single character from text file and store in a buffer. e.g file.get(ch); put() - writing a single character in textfile e.g. file.put(ch); getline() - read a line of text from text file store in a buffer. e.g file.getline(s,80); We can also use file>>ch for reading and file<<ch writing in text file. But >> operator does not accept white spaces.
  • 14. Program to create a text file using strings I/O
  • 16. Program to read content of file and display it
  • 17. Program to edit the content of file and display it
  • 19. Binary File Functions read( )- read a block of binary data or reads a fixed number of bytes from the specified stream and store in a buffer. Syntax : Stream_object.read((char *)& Object, sizeof(Object)); e.g file.read((char *)&s, sizeof(s)); write( ) – write a block of binary data or writes fixed number of bytes from a specific memory location to the specified stream. Syntax : Stream_object.write((char *)& Object, sizeof(Object)); e.g file.write((char *)&s, sizeof(s)); 19
  • 20. Binary File Functions Note: Both functions take two arguments. • The first is the address of variable, and the second is the length of that variable in bytes. The address of variable must be type cast to type char*(pointer to character type) • The data written to a file using write( ) can only be read accurately using read( ). 20
  • 21. Program to create a binary file
  • 22.
  • 23. File Pointer The file pointer indicates the position in the file at which the next input/output is to occur. Moving the file pointer in a file for various operations viz modification, deletion , searching etc. Following functions are used seekg(): It places the file pointer to the specified position in input mode of file. e.g file.seekg(p,ios::beg); or file.seekg(-p,ios::end), or file.seekg(p,ios::cur) i.e to move to p byte position from beginning, end or current position. 23
  • 24. File Pointer seekp(): It places the file pointer to the specified position in output mode of file. e.g file.seekp(p,ios::beg); or file.seekp(-p,ios::end), or file.seekp(p,ios::cur) i.e to move to p byte position from beginning, end or current position. tellg(): This function returns the current working position of the file pointer in the input mode. e.g int p=file.tellg(); tellp(): This function returns the current working position of the file pointer in the output mode. e.f int p=file.tellp(); 24