SlideShare a Scribd company logo
5
Most read
7
Most read
12
Most read
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

PPTX
File in C language
PDF
08. handling file streams
PPT
PPT
16717 functions in C++
 
PPTX
Built in function
DOC
C lab-programs
PPTX
classes and objects in C++
File in C language
08. handling file streams
16717 functions in C++
 
Built in function
C lab-programs
classes and objects in C++

What's hot (20)

PPTX
Data file handling in python introduction,opening & closing files
PPT
Strings
PPTX
File Management in C
PPTX
File handling in C
PPTX
Structure in C language
PDF
Problem Solving Techniques and Introduction to C
PPTX
Vim Editor And Basic Scripting (Ch-7)
PDF
chapter-8-function-overloading.pdf
PPT
File handling in C++
PPTX
Presentation on c structures
PDF
file handling c++
PPTX
File handling in c
PPTX
Input output statement in C
PDF
Problem solving methodology
PPTX
C Programming Unit-5
PDF
Memory Management C++ (Peeling operator new() and delete())
PPT
File in c
PPTX
07. Virtual Functions
Data file handling in python introduction,opening & closing files
Strings
File Management in C
File handling in C
Structure in C language
Problem Solving Techniques and Introduction to C
Vim Editor And Basic Scripting (Ch-7)
chapter-8-function-overloading.pdf
File handling in C++
Presentation on c structures
file handling c++
File handling in c
Input output statement in C
Problem solving methodology
C Programming Unit-5
Memory Management C++ (Peeling operator new() and delete())
File in c
07. Virtual Functions
Ad

Similar to File management in C++ (20)

PPT
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
PDF
Data file handling
PPTX
Data file handling in c++
PPTX
Introduction to files management systems
PDF
Filepointers1 1215104829397318-9
PPT
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
PPSX
Files in c++
PDF
Filesinc 130512002619-phpapp01
PPT
File handling in_c
PPTX
basics of file handling
PPTX
Basics of file handling
PPTX
Cs1123 10 file operations
PPT
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
PPT
file_handling_in_c.ppt......................................
PPTX
File handling
PPTX
File Handling
DOCX
Filehandling
PPTX
Chapter4.pptx
PPT
File Handling In C++(OOPs))
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
Data file handling
Data file handling in c++
Introduction to files management systems
Filepointers1 1215104829397318-9
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Files in c++
Filesinc 130512002619-phpapp01
File handling in_c
basics of file handling
Basics of file handling
Cs1123 10 file operations
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
file_handling_in_c.ppt......................................
File handling
File Handling
Filehandling
Chapter4.pptx
File Handling In C++(OOPs))
Ad

Recently uploaded (20)

PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Modernizing your data center with Dell and AMD
PDF
Advanced IT Governance
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Electronic commerce courselecture one. Pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Big Data Technologies - Introduction.pptx
PPT
Teaching material agriculture food technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
PPTX
Cloud computing and distributed systems.
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
Spectral efficient network and resource selection model in 5G networks
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Modernizing your data center with Dell and AMD
Advanced IT Governance
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Dropbox Q2 2025 Financial Results & Investor Presentation
Electronic commerce courselecture one. Pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
GamePlan Trading System Review: Professional Trader's Honest Take
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The AUB Centre for AI in Media Proposal.docx
Big Data Technologies - Introduction.pptx
Teaching material agriculture food technology
NewMind AI Weekly Chronicles - August'25 Week I
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
NewMind AI Monthly Chronicles - July 2025
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
Cloud computing and distributed systems.
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Spectral efficient network and resource selection model in 5G networks

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.