SlideShare a Scribd company logo
1 of 15
Download to read offline
FILE HANDLING IN C++
FILE CONCEPTS
• Every program or sub-program consists of
two major components:
• algorithm and
• data structures.
• The algorithm takes care of the rules and
procedures required for solving the problem
and the data structures contain the data.
• The data is manipulated by the procedures for
achieving the goals of the program as shown
in Fig.
Fig. The structure of a program or sub-program
A data structure is volatile by nature in the
sense that its contents are lost as soon as the
execution of the program is over.
• Similarly, an object also loses its states after
the program is over.
If we want to permanently store our data or
want to create persistent objects then it
becomes necessary to store the same in a
special data structure called file.
• The file can be stored on a second storage
media such as hard disk. In fact, vary large
data is always stored in a file.
File
“A file is a logical collection of records where each record
consists of a number of items known as fields”.
The records in a file can be arranged in the following three ways:
• Ascending/Descending order: The records in the file can be
arranged according to ascending or descending order of a key field..
• Alphabetical order: If the key field is of alphabetic type then the
records are arranged in alphabetical order.
• Chronological order: In this type of order, the records are stored in
the order of their occurrence i.e. arranged according to dates or
events. If the key-field is a date, i.e., date of birth, date of joining,
etc. then this type of arrangement is used.
FILES AND STREAMS
In C++, a stream is a data flow from a source to a sink. The
sources and sinks can be any of the input/output
devices or files.
For input and output, there are two different streams
called input stream and output stream.
Stream Description
cin standard input stream
cout standard output stream
cerr standard error stream
The standard source and sink are keyboard and monitor
screen respectively
ifstream: It is the input file stream class. Its member
function open( ) associates the stream with a specified
file in an input mode.
In addition to open(), ifstream class inherits the following functions
from istream class.
(i) get( ) (ii) getline( ) (iii) read( ) (iv) seekg( ) (iv) tellg( )
ofstream : It is the output file stream class. Its member
function open( ) associates the stream with a specified
file in an output mode.
In addition to open(), ofstream inherits the following functions from
ostream class
(i) put( ) (ii) write( ) (iii) seekp( ), (iv) tellp( )
fstream : It supports files for simultaneous input and
output. It is derived from ifstream, ofstream and
iostream classes.
The functions associated with this stream are :
1. open : This associates the stream with a specified file.
2. close : It closes the stream.
3. close all : It closes all the opened streams
4. seekg : Sets current `get' position in a stream
5. seekp : Sets current `put' position in a stream
6. tellg : Returns the current `get' position in a stream
7. tellp : Returns the current `put' position in a stream.
OPENING AND CLOSING A FILE
(Text Files)
A file can be opened in C++ by two methods:
1. By using the constructor of the stream
class to be used
2. By using the open( ) function of the stream
class to be used
For reading entire lines of text :
C++ provides get( ) and getline( ) functions as
input member functions of the ifstream class.
It also provides a put( ) function as output
member function of the ofstream class.
OPENING THE FILES BY USING
FUNCTION OPEN( )
ofstream newfile; ...(i)
newfile.open (“test.dat”); ...(ii)
In the statement
(i) declares the stream newfile to be of type ofstream i.e. output
stream. The statement .
(ii) assigns the file stream to the file called “test.dat”. Thus, in the
program the file “test.dat” would be known as newfile.
The major advantage of this method of opening a file is that
more than one files can be opened at a time in a program.
READING AND WRITING BLOCKS AND
OBJECTS(BINARY FILES)
The major advantage of binary files is that they require less
memory space for storage of data. Moreover, these files can be
used to read or write structured data such as structures, class
objects etc.
STORING OBJECTS IN FILES
If the information contained in the object is very
important then we must try to save it on auxiliary storage
such as hard disk so that it can be reused as and when
required.
Normally the contents of an object are lost as soon as the
object goes out of scope or the program execution is over.
In fact, similar to records, objects can also be written into
and instantiated from a file.
The objects which remember their data and information
are called as persistent objects.
DETECTING END OF FILE
While reading a file, a situation can arise when we do
not know the number of objects to be read from the
file i.e. we do not know where the file is going to end?
A simple method of detecting end of file (eof) is by
testing the stream in a while loop as shown below:
while (<stream>)
{
:
}
The condition <stream> will evaluate to 1 as long as
the end of file is not reached and it will return 0 as
soon as end of file is detected.
SUMMARY
• Any thing stored on a permanent storage is called a file.
• A set of related data items is known as a record. The
smallest unit of a record is called a field.
• A key field is used to uniquely identify a record.
• A file is a logical collection of records.
• In a serial file, the records are stored in the order of their
arrival without regards to the key field.
• On the other hand, in a sequential file, the records are
written in a particular order of the key field.
• The key field is also known as a primary key. 'ifstream’ and
‘ofstream’ are input and output streams respectively.
• The objects that remember their data and information are
called persistent objects.
• The function eof() returns 0 when it detects the end of
file. A opened file must be closed after its usage.

More Related Content

What's hot

File handling in_c
File handling in_cFile handling in_c
File handling in_csanya6900
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++Vineeta Garg
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ pptKumar
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams Ahmed Farag
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))Papu Kumar
 
working file handling in cpp overview
working file handling in cpp overviewworking file handling in cpp overview
working file handling in cpp overviewgourav kottawar
 
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
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handlingpinkpreet_kaur
 
08. handling file streams
08. handling file streams08. handling file streams
08. handling file streamsHaresh Jaiswal
 
file handling c++
file handling c++file handling c++
file handling c++Guddu Spy
 
Data file handling
Data file handlingData file handling
Data file handlingTAlha MAlik
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CAshim Lamichhane
 

What's hot (20)

File handling in_c
File handling in_cFile handling in_c
File handling in_c
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
 
Files in c++
Files in c++Files in c++
Files in c++
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Cpp file-handling
Cpp file-handlingCpp file-handling
Cpp file-handling
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 
working file handling in cpp overview
working file handling in cpp overviewworking file handling in cpp overview
working file handling in cpp overview
 
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)
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
08. handling file streams
08. handling file streams08. handling file streams
08. handling file streams
 
17 files and streams
17 files and streams17 files and streams
17 files and streams
 
Filesin c++
Filesin c++Filesin c++
Filesin c++
 
file handling c++
file handling c++file handling c++
file handling c++
 
Data file handling
Data file handlingData file handling
Data file handling
 
C++ files and streams
C++ files and streamsC++ files and streams
C++ files and streams
 
File handling
File handlingFile handling
File handling
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
 

Similar to Filehadnling

Similar to Filehadnling (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++
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
 
Data file handling
Data file handlingData file handling
Data file handling
 
File Handling
File HandlingFile Handling
File Handling
 
File management
File managementFile management
File management
 
File Handling
File HandlingFile Handling
File Handling
 
File Handling
File HandlingFile Handling
File Handling
 
File handling.pptx
File handling.pptxFile handling.pptx
File handling.pptx
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for reference
 
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
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
data file handling
data file handlingdata file handling
data file handling
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
working with files
working with filesworking with files
working with files
 

Recently uploaded

VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...Suhani Kapoor
 
VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...
VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...
VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...Suhani Kapoor
 
Gray Gold Clean CV Resume2024tod (1).pdf
Gray Gold Clean CV Resume2024tod (1).pdfGray Gold Clean CV Resume2024tod (1).pdf
Gray Gold Clean CV Resume2024tod (1).pdfpadillaangelina0023
 
Final Completion Certificate of Marketing Management Internship
Final Completion Certificate of Marketing Management InternshipFinal Completion Certificate of Marketing Management Internship
Final Completion Certificate of Marketing Management InternshipSoham Mondal
 
阿德莱德大学本科毕业证成绩单咨询(书英文硕士学位证)
阿德莱德大学本科毕业证成绩单咨询(书英文硕士学位证)阿德莱德大学本科毕业证成绩单咨询(书英文硕士学位证)
阿德莱德大学本科毕业证成绩单咨询(书英文硕士学位证)obuhobo
 
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Service
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Service
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Servicejennyeacort
 
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call GirlsDelhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girlsshivangimorya083
 
Storytelling, Ethics and Workflow in Documentary Photography
Storytelling, Ethics and Workflow in Documentary PhotographyStorytelling, Ethics and Workflow in Documentary Photography
Storytelling, Ethics and Workflow in Documentary PhotographyOrtega Alikwe
 
PM Job Search Council Info Session - PMI Silver Spring Chapter
PM Job Search Council Info Session - PMI Silver Spring ChapterPM Job Search Council Info Session - PMI Silver Spring Chapter
PM Job Search Council Info Session - PMI Silver Spring ChapterHector Del Castillo, CPM, CPMM
 
Business Development and Product Strategy for a SME named SARL based in Leban...
Business Development and Product Strategy for a SME named SARL based in Leban...Business Development and Product Strategy for a SME named SARL based in Leban...
Business Development and Product Strategy for a SME named SARL based in Leban...Soham Mondal
 
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证obuhobo
 
Preventing and ending sexual harassment in the workplace.pptx
Preventing and ending sexual harassment in the workplace.pptxPreventing and ending sexual harassment in the workplace.pptx
Preventing and ending sexual harassment in the workplace.pptxGry Tina Tinde
 
(Call Girls) in Lucknow Real photos of Female Escorts 👩🏼‍❤️‍💋‍👩🏻 8923113531 ➝...
(Call Girls) in Lucknow Real photos of Female Escorts 👩🏼‍❤️‍💋‍👩🏻 8923113531 ➝...(Call Girls) in Lucknow Real photos of Female Escorts 👩🏼‍❤️‍💋‍👩🏻 8923113531 ➝...
(Call Girls) in Lucknow Real photos of Female Escorts 👩🏼‍❤️‍💋‍👩🏻 8923113531 ➝...gurkirankumar98700
 
加利福尼亚艺术学院毕业证文凭证书( 咨询 )证书双学位
加利福尼亚艺术学院毕业证文凭证书( 咨询 )证书双学位加利福尼亚艺术学院毕业证文凭证书( 咨询 )证书双学位
加利福尼亚艺术学院毕业证文凭证书( 咨询 )证书双学位obuhobo
 
VIP Call Girls in Cuttack Aarohi 8250192130 Independent Escort Service Cuttack
VIP Call Girls in Cuttack Aarohi 8250192130 Independent Escort Service CuttackVIP Call Girls in Cuttack Aarohi 8250192130 Independent Escort Service Cuttack
VIP Call Girls in Cuttack Aarohi 8250192130 Independent Escort Service CuttackSuhani Kapoor
 
Black and White Minimalist Co Letter.pdf
Black and White Minimalist Co Letter.pdfBlack and White Minimalist Co Letter.pdf
Black and White Minimalist Co Letter.pdfpadillaangelina0023
 
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service Bhilai
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service BhilaiVIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service Bhilai
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service BhilaiSuhani Kapoor
 
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...Suhani Kapoor
 

Recently uploaded (20)

VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...
 
VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...
VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...
VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...
 
Gray Gold Clean CV Resume2024tod (1).pdf
Gray Gold Clean CV Resume2024tod (1).pdfGray Gold Clean CV Resume2024tod (1).pdf
Gray Gold Clean CV Resume2024tod (1).pdf
 
Final Completion Certificate of Marketing Management Internship
Final Completion Certificate of Marketing Management InternshipFinal Completion Certificate of Marketing Management Internship
Final Completion Certificate of Marketing Management Internship
 
阿德莱德大学本科毕业证成绩单咨询(书英文硕士学位证)
阿德莱德大学本科毕业证成绩单咨询(书英文硕士学位证)阿德莱德大学本科毕业证成绩单咨询(书英文硕士学位证)
阿德莱德大学本科毕业证成绩单咨询(书英文硕士学位证)
 
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Service
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Service
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Service
 
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call GirlsDelhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
 
Call Girls In Prashant Vihar꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCe
Call Girls In Prashant Vihar꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCeCall Girls In Prashant Vihar꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCe
Call Girls In Prashant Vihar꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCe
 
Storytelling, Ethics and Workflow in Documentary Photography
Storytelling, Ethics and Workflow in Documentary PhotographyStorytelling, Ethics and Workflow in Documentary Photography
Storytelling, Ethics and Workflow in Documentary Photography
 
PM Job Search Council Info Session - PMI Silver Spring Chapter
PM Job Search Council Info Session - PMI Silver Spring ChapterPM Job Search Council Info Session - PMI Silver Spring Chapter
PM Job Search Council Info Session - PMI Silver Spring Chapter
 
Business Development and Product Strategy for a SME named SARL based in Leban...
Business Development and Product Strategy for a SME named SARL based in Leban...Business Development and Product Strategy for a SME named SARL based in Leban...
Business Development and Product Strategy for a SME named SARL based in Leban...
 
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
 
FULL ENJOY Call Girls In Gautam Nagar (Delhi) Call Us 9953056974
FULL ENJOY Call Girls In Gautam Nagar (Delhi) Call Us 9953056974FULL ENJOY Call Girls In Gautam Nagar (Delhi) Call Us 9953056974
FULL ENJOY Call Girls In Gautam Nagar (Delhi) Call Us 9953056974
 
Preventing and ending sexual harassment in the workplace.pptx
Preventing and ending sexual harassment in the workplace.pptxPreventing and ending sexual harassment in the workplace.pptx
Preventing and ending sexual harassment in the workplace.pptx
 
(Call Girls) in Lucknow Real photos of Female Escorts 👩🏼‍❤️‍💋‍👩🏻 8923113531 ➝...
(Call Girls) in Lucknow Real photos of Female Escorts 👩🏼‍❤️‍💋‍👩🏻 8923113531 ➝...(Call Girls) in Lucknow Real photos of Female Escorts 👩🏼‍❤️‍💋‍👩🏻 8923113531 ➝...
(Call Girls) in Lucknow Real photos of Female Escorts 👩🏼‍❤️‍💋‍👩🏻 8923113531 ➝...
 
加利福尼亚艺术学院毕业证文凭证书( 咨询 )证书双学位
加利福尼亚艺术学院毕业证文凭证书( 咨询 )证书双学位加利福尼亚艺术学院毕业证文凭证书( 咨询 )证书双学位
加利福尼亚艺术学院毕业证文凭证书( 咨询 )证书双学位
 
VIP Call Girls in Cuttack Aarohi 8250192130 Independent Escort Service Cuttack
VIP Call Girls in Cuttack Aarohi 8250192130 Independent Escort Service CuttackVIP Call Girls in Cuttack Aarohi 8250192130 Independent Escort Service Cuttack
VIP Call Girls in Cuttack Aarohi 8250192130 Independent Escort Service Cuttack
 
Black and White Minimalist Co Letter.pdf
Black and White Minimalist Co Letter.pdfBlack and White Minimalist Co Letter.pdf
Black and White Minimalist Co Letter.pdf
 
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service Bhilai
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service BhilaiVIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service Bhilai
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service Bhilai
 
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...
 

Filehadnling

  • 2. FILE CONCEPTS • Every program or sub-program consists of two major components: • algorithm and • data structures. • The algorithm takes care of the rules and procedures required for solving the problem and the data structures contain the data. • The data is manipulated by the procedures for achieving the goals of the program as shown in Fig.
  • 3. Fig. The structure of a program or sub-program
  • 4. A data structure is volatile by nature in the sense that its contents are lost as soon as the execution of the program is over. • Similarly, an object also loses its states after the program is over. If we want to permanently store our data or want to create persistent objects then it becomes necessary to store the same in a special data structure called file. • The file can be stored on a second storage media such as hard disk. In fact, vary large data is always stored in a file.
  • 5. File “A file is a logical collection of records where each record consists of a number of items known as fields”. The records in a file can be arranged in the following three ways: • Ascending/Descending order: The records in the file can be arranged according to ascending or descending order of a key field.. • Alphabetical order: If the key field is of alphabetic type then the records are arranged in alphabetical order. • Chronological order: In this type of order, the records are stored in the order of their occurrence i.e. arranged according to dates or events. If the key-field is a date, i.e., date of birth, date of joining, etc. then this type of arrangement is used.
  • 6. FILES AND STREAMS In C++, a stream is a data flow from a source to a sink. The sources and sinks can be any of the input/output devices or files. For input and output, there are two different streams called input stream and output stream. Stream Description cin standard input stream cout standard output stream cerr standard error stream The standard source and sink are keyboard and monitor screen respectively
  • 7. ifstream: It is the input file stream class. Its member function open( ) associates the stream with a specified file in an input mode. In addition to open(), ifstream class inherits the following functions from istream class. (i) get( ) (ii) getline( ) (iii) read( ) (iv) seekg( ) (iv) tellg( ) ofstream : It is the output file stream class. Its member function open( ) associates the stream with a specified file in an output mode. In addition to open(), ofstream inherits the following functions from ostream class (i) put( ) (ii) write( ) (iii) seekp( ), (iv) tellp( )
  • 8. fstream : It supports files for simultaneous input and output. It is derived from ifstream, ofstream and iostream classes. The functions associated with this stream are : 1. open : This associates the stream with a specified file. 2. close : It closes the stream. 3. close all : It closes all the opened streams 4. seekg : Sets current `get' position in a stream 5. seekp : Sets current `put' position in a stream 6. tellg : Returns the current `get' position in a stream 7. tellp : Returns the current `put' position in a stream.
  • 9. OPENING AND CLOSING A FILE (Text Files) A file can be opened in C++ by two methods: 1. By using the constructor of the stream class to be used 2. By using the open( ) function of the stream class to be used
  • 10. For reading entire lines of text : C++ provides get( ) and getline( ) functions as input member functions of the ifstream class. It also provides a put( ) function as output member function of the ofstream class.
  • 11. OPENING THE FILES BY USING FUNCTION OPEN( ) ofstream newfile; ...(i) newfile.open (“test.dat”); ...(ii) In the statement (i) declares the stream newfile to be of type ofstream i.e. output stream. The statement . (ii) assigns the file stream to the file called “test.dat”. Thus, in the program the file “test.dat” would be known as newfile. The major advantage of this method of opening a file is that more than one files can be opened at a time in a program.
  • 12. READING AND WRITING BLOCKS AND OBJECTS(BINARY FILES) The major advantage of binary files is that they require less memory space for storage of data. Moreover, these files can be used to read or write structured data such as structures, class objects etc.
  • 13. STORING OBJECTS IN FILES If the information contained in the object is very important then we must try to save it on auxiliary storage such as hard disk so that it can be reused as and when required. Normally the contents of an object are lost as soon as the object goes out of scope or the program execution is over. In fact, similar to records, objects can also be written into and instantiated from a file. The objects which remember their data and information are called as persistent objects.
  • 14. DETECTING END OF FILE While reading a file, a situation can arise when we do not know the number of objects to be read from the file i.e. we do not know where the file is going to end? A simple method of detecting end of file (eof) is by testing the stream in a while loop as shown below: while (<stream>) { : } The condition <stream> will evaluate to 1 as long as the end of file is not reached and it will return 0 as soon as end of file is detected.
  • 15. SUMMARY • Any thing stored on a permanent storage is called a file. • A set of related data items is known as a record. The smallest unit of a record is called a field. • A key field is used to uniquely identify a record. • A file is a logical collection of records. • In a serial file, the records are stored in the order of their arrival without regards to the key field. • On the other hand, in a sequential file, the records are written in a particular order of the key field. • The key field is also known as a primary key. 'ifstream’ and ‘ofstream’ are input and output streams respectively. • The objects that remember their data and information are called persistent objects. • The function eof() returns 0 when it detects the end of file. A opened file must be closed after its usage.