File Handling in CPP Programming Language.pptx

SajjadAbdullah4 11 views 12 slides Jun 06, 2024
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

How to handle files in C++ Programming Language...


Slide Content

Files File is a collection of related records. Permanent Storage User Input -> Variables -> Program -> File File -> Data Read to Program Types of Files Text Files (readable & printable characters) Binary Files (non-readable binary code)

File Access Methods Sequential Access Method Have to read all record in order to read a later byte Slow Method Variable Length Records, Memory is not wasted Random Access Method Direct access Faster Method Fixed Length Records, Memory wasted

Stream A stream is a series of bytes associated with a file. Exchange of data between program and file in the form of bytes. Types of Streams Standard Input Stream Connection between standard input device and program Act of reading data from an input stream is called Extraction . Uses >> operator. E.g. cin object, part of istream Standard Output Stream Connection between standard output device and program. Act of writing data to an output stream is called Insertion . Uses << operator. E.g. cout object, part of ostream

Predefined Stream Objects cin -> File -> istream cout -> File -> ostream cerr -> Monitor -> ostream clog -> Monitor -> ostream These objects can be redirected to other devices or files.

Stream Class Hierarchy ios istream ostream ifstream iostream ofstream fstream

Opening Files File Pointer Ifstream , ofstream , fstream open() function File Opening Modes ios ::in input mode ios ::out output mode ios:binary binary mode ios:ate sets file pointer at the end of the file , input/output mode ios ::app append, only used in output mode ios :: trunc deletes previous data (if any) of the file ios :: nocreate opens file only if it exists ios :: noreplace opens file only if it does not exist Multiple modes can also be specified using | symbol.

Default File Opening Modes ios ::in for ifstream file pointer ios ::out for ofstream file pointer ios ::in | ios ::out for fstream file pointer

Verifying File Opening is_open () function ! fp

Closing Files close() function

Formatted Files I/O Series of characters Each character takes one byte 38.125 will take 6 bytes . Not storage efficient Easy to implement End of File Detection eof () function

Character File I/O Only one character at a time put() function get() function

Binary File I/O Efficient data storage write() function read() function
Tags