SlideShare a Scribd company logo
1 of 14
By,
Apoorva Verma
What is a File?
A file is bunch of bytes stored on some storage device
like tape, magnetic disk etc.
C++ File Classes
In C++, file input/output facilities are implemented using three
classes-
1. fstream: Stream class to both read and write from/to files.
2. ifstream: Stream class to read from files
3. ofstream: Stream class to write on files
fstream header file
The C++ input/output operations are very much similar to the
console input and output operations.
The file operations also make use of streams as an interface
between the programs and the files.
A stream is a general name given to a flow of data at the
lowest level.
A stream can be just defined as a sequence of bytes.
File Input Output Streams
From the above figure we can see that the stream that supplies data
to the program is known as input stream. It reads the data from the
file and hands it over to the program. The stream that receives data
from the program is known as output stream. It writes the received
data to the files.
Storage of Data Files
Data files can be stored in two ways-
1. Text file – This file stores information in ASCII characters. In text
files, each line of text is terminated with a special character
known as EOL(End of line) character.
2. Binary file - It contains information in the same format in
which the information is held in memory . In binary files , there
is no delimiter for the line. Also no translation occurs in binary
files.
Opening a File
A file must be opened before you can read from it or write to it.
Either ofstream or fstream object may be used to open a file for
writing. And ifstream object is used to open a file for reading
purpose only.
Syntax-
void open(const char *filename, ios::openmode mode);
Here, the first argument specifies the name and location of the file
to be opened and the second argument of the open() member
function defines the mode in which the file should be opened.
S.No Mode Flag & Description
1 ios::app
Append mode. All output to that file to be appended to the end.
2 ios::ate
Open a file for output and move the read/write control to the end of the file.
3 ios::in
Open a file for reading.
4 ios::out
Open a file for writing.
5 ios::trunc
If the file already exists, its contents will be truncated before opening the file.
Example
Example
ofstream outfile;
outfile.open("file.dat", ios::out | ios::trunc );
Similar way, you can open a file for reading and writing purpose
as follows −
fstream afile;
afile. Open("file.dat", ios::out | ios::in );
When a C++ program terminates it automatically flushes all
the streams, release all the allocated memory and close all
the opened files. But it is always a good practice that a
programmer should close all the opened files before program
termination.
Syntax-
void close();
Closing a File
Writing to a File
While doing C++ programming, you write information to a file from your program using
the stream insertion operator (<<) just as you use that operator to output information
to the screen. The only difference is that you use an ofstream or fstream object instead
of the cout object
#include <iostream.h>
#include <fstream . h>
using namespace std;
void main() {
ofstream MyFile("file1.txt"); // Create and open a text file
MyFile << "Files can be tricky, but it is fun enough!"; // Write to the file
MyFile. Close(); // Close the file
}
Sample Code
Handling Data Files
Data files in C++ can be handles in two ways
1. Sequential file handling –A sequential access is that in which the
records are accessed in some sequence, i.e., the information in the
file is processed in order, one record after the other. This access
method is the most primitive one.
2. Random File Handling - Random-access file is a term used to
describe a file or set of files that are accessed directly instead of
requiring that other files be read first. A random-access data file
enables you to read or write information anywhere in the file.
Sequential I/O with files
• The file stream classes support a number of member functions for performing the input and
output operations on files.
• The functions get() and put() are capable of handling a single character at a time.
• The function getline() lets you handle multiple characters at a time.
•Another pair of functions read() and write() are capable of reading and writing blocks of binary
data.
The get(), getline() and put() Functions The functions get() and put() are byte-oriented. That is,
get() will read a byte of data and put() will write a byte of data. The get() has many forms, but
the most commonly used version is shown here, along with put() :
istream & get(char & ch) ; //prototype of put()
istream & getline(char * buf, int num, char delim = 'n') ; //prototype of getline()
Random I/O with Files
The seekg(), seekp(), tellg() and tellp() Functions
In C++, random access is achieved by manipulating seekg(), seekp(), tellg() and
tellp() functions. The seekg() and tellg() functions allow you to set and examine
the get_pointer, and the seekp() and tellp() functions perform these operations on
the put_pointer.
The seekg() and tellg() functions are for input streams (ifstream) and seekp() and
tellp() functions are for output streams (ofstream). However, if you use them with
an fstream object then tellg() and tellp() return the same value. Also seekg() and
seekp() work the same way in an fstream object.

More Related Content

What's hot (20)

OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++
 
operators and expressions in c++
 operators and expressions in c++ operators and expressions in c++
operators and expressions in c++
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
Classes,object and methods java
Classes,object and methods javaClasses,object and methods java
Classes,object and methods java
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
Encapsulation in C++
Encapsulation in C++Encapsulation in C++
Encapsulation in C++
 
Control statements
Control statementsControl statements
Control statements
 
Class and object
Class and objectClass and object
Class and object
 
Types of c operators ppt
Types of c operators pptTypes of c operators ppt
Types of c operators ppt
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
inheritance
inheritanceinheritance
inheritance
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition Presentation
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Storage classes
Storage classesStorage classes
Storage classes
 
File in C language
File in C languageFile in C language
File in C language
 

Similar to File management in C++

chapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfchapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfstudy material
 
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
 
File handling in_c
File handling in_cFile handling in_c
File handling in_csanya6900
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++Shyam Gupta
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams Ahmed Farag
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Rex Joe
 
Data file handling
Data file handlingData file handling
Data file handlingTAlha MAlik
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5YOGESH SINGH
 
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 exampleSunil Patel
 
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
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++Vineeta Garg
 
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
 

Similar to File management in C++ (20)

chapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfchapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdf
 
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 handling in_c
File handling in_cFile handling in_c
File handling in_c
 
File Handling
File HandlingFile Handling
File Handling
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
Files in c++
Files in c++Files in c++
Files in c++
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
 
Data file handling
Data file handlingData file handling
Data file handling
 
Filehadnling
FilehadnlingFilehadnling
Filehadnling
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
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 Handling in C++
File Handling in C++File Handling in C++
File Handling 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++
 
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
 

Recently uploaded

Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 

Recently uploaded (20)

Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 

File management in C++

  • 2. What is a File? A file is bunch of bytes stored on some storage device like tape, magnetic disk etc.
  • 3. C++ File Classes In C++, file input/output facilities are implemented using three classes- 1. fstream: Stream class to both read and write from/to files. 2. ifstream: Stream class to read from files 3. ofstream: Stream class to write on files
  • 4. fstream header file The C++ input/output operations are very much similar to the console input and output operations. The file operations also make use of streams as an interface between the programs and the files. A stream is a general name given to a flow of data at the lowest level. A stream can be just defined as a sequence of bytes.
  • 5. File Input Output Streams From the above figure we can see that the stream that supplies data to the program is known as input stream. It reads the data from the file and hands it over to the program. The stream that receives data from the program is known as output stream. It writes the received data to the files.
  • 6. Storage of Data Files Data files can be stored in two ways- 1. Text file – This file stores information in ASCII characters. In text files, each line of text is terminated with a special character known as EOL(End of line) character. 2. Binary file - It contains information in the same format in which the information is held in memory . In binary files , there is no delimiter for the line. Also no translation occurs in binary files.
  • 7. Opening a File A file must be opened before you can read from it or write to it. Either ofstream or fstream object may be used to open a file for writing. And ifstream object is used to open a file for reading purpose only. Syntax- void open(const char *filename, ios::openmode mode); Here, the first argument specifies the name and location of the file to be opened and the second argument of the open() member function defines the mode in which the file should be opened.
  • 8. S.No Mode Flag & Description 1 ios::app Append mode. All output to that file to be appended to the end. 2 ios::ate Open a file for output and move the read/write control to the end of the file. 3 ios::in Open a file for reading. 4 ios::out Open a file for writing. 5 ios::trunc If the file already exists, its contents will be truncated before opening the file.
  • 9. Example Example ofstream outfile; outfile.open("file.dat", ios::out | ios::trunc ); Similar way, you can open a file for reading and writing purpose as follows − fstream afile; afile. Open("file.dat", ios::out | ios::in );
  • 10. When a C++ program terminates it automatically flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination. Syntax- void close(); Closing a File
  • 11. Writing to a File While doing C++ programming, you write information to a file from your program using the stream insertion operator (<<) just as you use that operator to output information to the screen. The only difference is that you use an ofstream or fstream object instead of the cout object #include <iostream.h> #include <fstream . h> using namespace std; void main() { ofstream MyFile("file1.txt"); // Create and open a text file MyFile << "Files can be tricky, but it is fun enough!"; // Write to the file MyFile. Close(); // Close the file } Sample Code
  • 12. Handling Data Files Data files in C++ can be handles in two ways 1. Sequential file handling –A sequential access is that in which the records are accessed in some sequence, i.e., the information in the file is processed in order, one record after the other. This access method is the most primitive one. 2. Random File Handling - Random-access file is a term used to describe a file or set of files that are accessed directly instead of requiring that other files be read first. A random-access data file enables you to read or write information anywhere in the file.
  • 13. Sequential I/O with files • The file stream classes support a number of member functions for performing the input and output operations on files. • The functions get() and put() are capable of handling a single character at a time. • The function getline() lets you handle multiple characters at a time. •Another pair of functions read() and write() are capable of reading and writing blocks of binary data. The get(), getline() and put() Functions The functions get() and put() are byte-oriented. That is, get() will read a byte of data and put() will write a byte of data. The get() has many forms, but the most commonly used version is shown here, along with put() : istream & get(char & ch) ; //prototype of put() istream & getline(char * buf, int num, char delim = 'n') ; //prototype of getline()
  • 14. Random I/O with Files The seekg(), seekp(), tellg() and tellp() Functions In C++, random access is achieved by manipulating seekg(), seekp(), tellg() and tellp() functions. The seekg() and tellg() functions allow you to set and examine the get_pointer, and the seekp() and tellp() functions perform these operations on the put_pointer. The seekg() and tellg() functions are for input streams (ifstream) and seekp() and tellp() functions are for output streams (ofstream). However, if you use them with an fstream object then tellg() and tellp() return the same value. Also seekg() and seekp() work the same way in an fstream object.