0% found this document useful (0 votes)
34 views22 pages

C - Working With Files

Uploaded by

kunalchalke2827
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views22 pages

C - Working With Files

Uploaded by

kunalchalke2827
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Object-Oriented Programming

in C++

Working with Files


Prepared by
Mrs. Pallavi S. Bhamre
Junior Computer Science
Thkaur College of Science & Commerce
SYJC
COMPUTER SCIENCE I
CHAPTER
OOP IN C++
Mrs. Pallavi Bhamre

Knowledge is power
Working With Files
Topics to be covered :
 Introduction
 Input stream and output streams
 File stream Hierarchy
 How to open a file
 File pointers
 File modes
 Hands on Training
 Questionnaire
Working with Files : INTRODUCTION *

Definition : A file is a media for permanent storage.

A file can be of any type, binary file , txt file, cpp file etc.
Use : A file can be used to store input / output.

That is, we can redirect input / output from / to a file.


Stream is a buffer / interface between program and I/O devices
File streams: INTRODUCTION **

Definition : Stream is an interface between program and Input /


Output devices.

It is of two types :

1. Input Stream

2. Output Stream
File streams: INTRODUCTION **

1. Input Stream

Definition : A stream that supplies data to the program is known


as Input Stream.

Input Stream extracts / reads data from file.

So reading data from file is an input operation.


File streams: INTRODUCTION *

2. Output Stream

Definition : A stream that receives data from the program is


known as Output Stream.

Output Stream inserts / writes data to the file.

So writing data to file is an output operation.


Program Inter action with standard Input and Output Devices
Write
output on
Screen

Input Stream
Output
Input
InputStream
Stream
OutputScreen
Screen
Screen
Program

Keyboard
Input Stream

Take input Read input


from from
Keyboard Keyboard

- Standard Input stream and Output Stream in C++ -


File Input and output Stream **
Working with Files : INTRODUCTION
How to achieve File Handling:-
For achieving file handling in C++ we need follow following steps :
• Naming a file
• Opening a file
• Reading data from file
• Writing data into file
• Closing a file.
File Handling classes : fstream *
Input and output for files , that is , read and write for a file is handled by
member functions of three classes :
1. fstream : Supports simultaneous input / output operations for files.
2. ofstream : Supports output operations for files.
ofstream consists of predefined functions : open(),write(), put(), seekp(),
tellp(), close().
3. ifstream: Supports input operations for files.
ifstream consists of predefined functions : open(),read(),get(), seekg(),
tellg(), close().
Note : To do a specific operation for file , file should be opened a particular
mode.
File stream classes / file stream hierarchy **
File stream classes / file stream hierarchy * *
Working with Files : File modes *
• fstream:-It performs both read and write operations of file.
• a)Open():-This function is used to open a file in various modes.
Syntax :
File_stream_class stream_object ;
stream_object.open ("file name",[modes]);
Various modes in which we can open a file are:

Mode Meaning Purpose

ios :: out write Open the file for write only.

ios :: in read Open the file for read only.

ios :: app append Open the file for appending data to end-of-file.
File handling functions / methods
a) open () :
Syntax :
File_stream_class stream_object ;
stream_object.open ("file name",[modes]);
b) Write(): This function is used to read data from primary/ main
memory and write it into secondary memory.
It is used to read data from memory variable and write it into secondary
memory.
c)Read():-This function read data from secondary memory and write it
into memory variable.
d) eof():-This function will return true(1) at end of file otherwise false(0).
e) close():-Use to close the file.
File stream classes **
Basic / default file modes

class default mode parameter

ofstream ios::out

ifstream ios::in

fstream ios::in | ios::out


File pointers and their manipulations *
Each file has pointers :
1. get pointer : It is input pointer. Used to read information from file.
2. put pointer : It is output pointer. Used to write information on file.
Following file functions process on file pointers for file operations :
Name of Use
function
seekg () Moves get pointer (input) to specific location.

seekp () Moves put pointer (output) to specific location.

tellg () Gives the current position of the get pointer.

tellp () Gives the current position of the put pointer.


File Mode Parameters

ios::in Open file for input operations . That is , for reading only.
ios::out Open file for output operations. That is , for writing only.
ios::binary Open file in binary mode.
ios::ate Go to the end of file on opening.
ios::app Append to the end of file
ios::trunc Delete contents of file, if it exists
ios::nocreate Open fails if the file does not exist
ios::noreplace Open fails if the file already exists
Questionnaire : Working with files **
A. Theory Questions :
1. Explain file stream classes / hierarchy. (Board Theory) **
2. Explain input stream and output stream for file. **
3. Define input stream and output stream and explain their uses for
file operations. **
4. Define and explain file mode pointers.
5. Differentiate between :
• Input stream and output stream
• Read and write mode for file
• Ifstream and ofstream
• seekg() and seekp()
• tellg () and tellp()
Questionnaire : Working with files *
3.Define the following:
1. stream
2. Input stream
3. Output stream
4. ifstream
5. ofstream
6. fstream
7. seekg()
8. seekp()
9. tellg()
10. tellp()
11. opening a file in read mode
12. opening a file in write mode
END OF MODULE

Topic : Working with files in C++

You might also like