SlideShare a Scribd company logo
File handling
.
File handling
It is a file that 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 or delimiter
character. When this EOL character is read
or written, certain internal translations take
place.
It is a file that contains information in the
same format as it is held in memory. In
binary files, no delimiters are used for a line
and no translations occur here.
ofstream: Stream class to write on
files
ifstream: Stream class to read
from files
fstream: Stream class to both read
and write from/to files.
Opening a file
ofstream outFile("sample.txt"); //output only
ifstream inFile(“sample.txt”); //input only
Stream-object.open(“filename”, mode)
ofstream outFile;
outFile.open("sample.txt");
ifstream inFile;
inFile.open("sample.txt");
File handling
outFile.close();
inFile.close();
put() and get() function
the function put() writes a single character to the associated stream.
Similarly, the function get() reads a single character form the associated
stream.
example :
file.get(ch);
file.put(ch);
write() and read() function
write() and read() functions write and read blocks of binary data.
example:
file.read((char *)&obj, sizeof(obj));
file.write((char *)&obj, sizeof(obj));
File handling
All i/o streams objects have, at least, one internal stream
pointer:
ifstream, like istream, has a pointer known as the get
pointer that points to the element to be read in the next
input operation.
ofstream, like ostream, has a pointer known as the put
pointer that points to the location where the next
element has to be written.
Finally, fstream, inherits both, the get and the put
pointers, from iostream (which is itself derived from both
istream and ostream).
These internal stream pointers that point to the reading
or writing locations within a stream can be manipulated
using the following member functions:
The other prototype for these functions is:
seekg(offset, refposition );
seekp(offset, refposition );
The parameter offset represents the number of bytes
the file pointer is to be moved from the location
specified by the parameter refposition. The refposition
takes one of the following three constants defined in the
ios class.
ios::beg start of the file
ios::cur current position of the pointer
ios::end end of the file
example:
file.seekg(-10, ios::cur);
File I/O is a five-step process:
1. Include the header file fstream in the
program.
2. Declare file stream object.
3. Open the file with the file stream object.
4. Use the file stream object with >>, <<, or
other input/output functions.
5. Close the files.
When data is stored in a file in the binary
format, reading and writing
data is faster because no time is lost in
converting the data from one format to
another format. Such files are called binary
files. This following program explains how to
create binary files and also how to read,
write, search, delete and modify data from
binary files.
File handling

More Related Content

PDF
Chapter28 data-file-handling
DOCX
Filehandling
PPTX
File Handling
PPTX
Chapter 10.3
PPT
Javaio
PPS
Files & IO in Java
PDF
Java IO
Chapter28 data-file-handling
Filehandling
File Handling
Chapter 10.3
Javaio
Files & IO in Java
Java IO

What's hot (20)

PPTX
Stream classes in C++
PPTX
2CPP17 - File IO
PPTX
Streams and Files
PPTX
File management in C++
PDF
PDF
C++ Files and Streams
PPT
Input output streams
PDF
Basic i/o & file handling in java
PPT
File handling
PPT
17 files and streams
PPTX
Reading and Writing Files
PPT
Filehandlinging cp2
PPT
File Input & Output
PDF
Java Course 8: I/O, Files and Streams
PPTX
Understanding java streams
PPTX
Pf cs102 programming-8 [file handling] (1)
PPT
Mesics lecture files in 'c'
PPT
Byte stream classes.49
PPTX
C# File IO Operations
PPT
14 file handling
 
Stream classes in C++
2CPP17 - File IO
Streams and Files
File management in C++
C++ Files and Streams
Input output streams
Basic i/o & file handling in java
File handling
17 files and streams
Reading and Writing Files
Filehandlinging cp2
File Input & Output
Java Course 8: I/O, Files and Streams
Understanding java streams
Pf cs102 programming-8 [file handling] (1)
Mesics lecture files in 'c'
Byte stream classes.49
C# File IO Operations
14 file handling
 
Ad

Similar to File handling (20)

PPT
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
PDF
Data file handling
PDF
Input File dalam C++
PPT
7 Data File Handling
PDF
File handling
PDF
Files in C++.pdf is the notes of cpp for reference
PPTX
UNIT 10. Files and file handling in C
PDF
Filesinc 130512002619-phpapp01
PPSX
Files in c++
PPTX
Basics of file handling
PPTX
basics of file handling
PDF
FILES IN C
PPT
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
PPT
data file handling
PDF
chapter-12-data-file-handling.pdf
PPTX
File handling in c
PPTX
File Handling ppt.pptx shjd dbkd z bdjdb d
PDF
4 text file
PPTX
FILE HANDLING COMPUTER SCIENCE -FILES.pptx
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Data file handling
Input File dalam C++
7 Data File Handling
File handling
Files in C++.pdf is the notes of cpp for reference
UNIT 10. Files and file handling in C
Filesinc 130512002619-phpapp01
Files in c++
Basics of file handling
basics of file handling
FILES IN C
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
data file handling
chapter-12-data-file-handling.pdf
File handling in c
File Handling ppt.pptx shjd dbkd z bdjdb d
4 text file
FILE HANDLING COMPUTER SCIENCE -FILES.pptx
Ad

Recently uploaded (20)

PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Encapsulation theory and applications.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Empathic Computing: Creating Shared Understanding
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPT
Teaching material agriculture food technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
KodekX | Application Modernization Development
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Electronic commerce courselecture one. Pdf
Spectral efficient network and resource selection model in 5G networks
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Encapsulation theory and applications.pdf
The AUB Centre for AI in Media Proposal.docx
Review of recent advances in non-invasive hemoglobin estimation
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Empathic Computing: Creating Shared Understanding
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Teaching material agriculture food technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
“AI and Expert System Decision Support & Business Intelligence Systems”
KodekX | Application Modernization Development
Bridging biosciences and deep learning for revolutionary discoveries: a compr...

File handling

  • 2. .
  • 4. It is a file that 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 or delimiter character. When this EOL character is read or written, certain internal translations take place. It is a file that contains information in the same format as it is held in memory. In binary files, no delimiters are used for a line and no translations occur here.
  • 5. ofstream: Stream class to write on files ifstream: Stream class to read from files fstream: Stream class to both read and write from/to files.
  • 6. Opening a file ofstream outFile("sample.txt"); //output only ifstream inFile(“sample.txt”); //input only Stream-object.open(“filename”, mode) ofstream outFile; outFile.open("sample.txt"); ifstream inFile; inFile.open("sample.txt");
  • 8. outFile.close(); inFile.close(); put() and get() function the function put() writes a single character to the associated stream. Similarly, the function get() reads a single character form the associated stream. example : file.get(ch); file.put(ch); write() and read() function write() and read() functions write and read blocks of binary data. example: file.read((char *)&obj, sizeof(obj)); file.write((char *)&obj, sizeof(obj));
  • 10. All i/o streams objects have, at least, one internal stream pointer: ifstream, like istream, has a pointer known as the get pointer that points to the element to be read in the next input operation. ofstream, like ostream, has a pointer known as the put pointer that points to the location where the next element has to be written. Finally, fstream, inherits both, the get and the put pointers, from iostream (which is itself derived from both istream and ostream).
  • 11. These internal stream pointers that point to the reading or writing locations within a stream can be manipulated using the following member functions:
  • 12. The other prototype for these functions is: seekg(offset, refposition ); seekp(offset, refposition ); The parameter offset represents the number of bytes the file pointer is to be moved from the location specified by the parameter refposition. The refposition takes one of the following three constants defined in the ios class. ios::beg start of the file ios::cur current position of the pointer ios::end end of the file example: file.seekg(-10, ios::cur);
  • 13. File I/O is a five-step process: 1. Include the header file fstream in the program. 2. Declare file stream object. 3. Open the file with the file stream object. 4. Use the file stream object with >>, <<, or other input/output functions. 5. Close the files.
  • 14. When data is stored in a file in the binary format, reading and writing data is faster because no time is lost in converting the data from one format to another format. Such files are called binary files. This following program explains how to create binary files and also how to read, write, search, delete and modify data from binary files.