0% found this document useful (0 votes)
13 views24 pages

File Handling

File handling for Computer Science

Uploaded by

akhileshsalunke4
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)
13 views24 pages

File Handling

File handling for Computer Science

Uploaded by

akhileshsalunke4
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/ 24

Voc.

Computer Science Paper I

C++
OOP Basics

Mrs. Minakshi Godbole

Fergusson College Jr. Wing


Pune.
File Handling
Stream
• The I/O system in C++ is designed to work with wide variety
of devices including terminals, disks etc. Although each device
is very different, the I/O system supplies an interface to the
programmer that is independent of the actual device
being accessed. This interface is known as stream.

• Stream is a sequence of bytes.

• It acts either as source from which input data can be obtained


(known as input stream)

• Or destination to which output data can be sent. (known as


output stream)
Data Streams
Input stream

Input Device

Program

Output stream

Output device

•Keyboard and screen are default options for i/p and o/p
respectively.
•We can redirect streams to other devices or files also if
required.
File handling in C++
• The data is stored in the devices(hard disk, floppy
disk, CD etc.) using the concept of files. A file is a
collection of related data stored in the particular
area on the disk. Programs can be designed to
perform read and write operations on these files.

• We know the technique of handling the data


communications between the console unit and the
program. (Using cin, cout). Various file handling
methods are available for storing and retrieving
the data from the files.
File Input and output streams
• The input stream (for reading from file)
involves the creation of an input stream and
linking it with the program and the input file.

• The output stream (for writing to file) involves


the creation of an output stream and linking it
with the program and the output file.
File Input and output streams
Input stream
Read data

data Input

Disk Files Program

data Output
Output stream
Write data
Classes For File Stream Operations
• The I/O system of C++ contains a set of classes that
define the file handling methods. These classes
include ifstream, ofstream and fstream.

• These classes are derived from fstreambase and


from iostream class.
Classes For File Stream Operations

ios

istream streambuf ostream

iostream

ifstream ofstream filebuf


fstream

streambuf
• fstream class:- provides simultaneous input and output
operations. It contains open () with default input mode
and inherits all the functions from istream and ostream
through iostream class.
• ifstream class - provides input operations. Contains
open( ) with default input mode, inherits get (), getline(),
read(), seekg(), close(), tellg() functions through istream
class.
• ofstream class - provides output operations. Contains
open () with default output mode. Inherits functions like
put (), seekp(), tellp(), write() functions through ostream
class
• fstream base: provides operations common to
file stream. Serves as base for fstream ,
ifstream, and ofstream classes. Contains open
and close functons.

• filebuf : It’s purpose is to set file buffers to


read and write modes. Contains open and close
functions.
Opening a file in Intput mode(Reading)
Syntax :
File_stream_class stream_object;
stream_object . open(“file name”);

e.g.
ifstream fin1; /*ifstrem is File_stream_class
fin1 is a stream_object */

fin1.open (“Country.txt”);
/* This opens a file Country.txt
in input mode i.e. for reading*/
Opening a file in Output mode (Writing)
Syntax:
File_stream_class stream_object;
stream_object . open(“file name”);

e.g.
ofstream fout1; /*ofstrem is File_stream_class
fout1 is a stream_object */

fout1.open (“Country.txt”);
/* This opens a file Country.txt in
output mode i.e. for writing*/
Detecting END-OF-FILE
• Detecting end-of-file is necessary for preventing any
further attempt to read data from the file.

• An ifstream object such as fin1 returns a value of 0 if any


error occurs in the file operation including the end of file
condition.

• In the program we generally use statement like while(fin1)

• The while loop terminates when fin1 returns value of zero


on reaching the end-of-file condition.

• This loop may terminate due to any other failure in opening


file as well.
End-of-file using eof()
• See the example
if(fin1.eof() != 0)
exit(1);
• eof() is a member function of ios class.
• It returns a non-zero value if the end-of-file
condition is encountered and a zero otherwise
• So the above statement terminates the program
on reaching the end of the file.
Closing a file

syntax :-
Stream_object . close();

e.g fout1.close();

/*This closes file linked to fout1 object. Here


Country.txt will be closed.*/
Program for file handling in C++
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
void main()
{clrscr();
int i,n;
const int size = 20;
char countryarr[size];
char capitalarr[size];
ifstream fin1, fin2; //Creating a object of ifstream class for reading from the file

ofstream fout1, fout2; //Creating a object of ofstream class for writing to the file

fout1.open("Country.txt"); //Opening the file for write operations (output


mode)

fout2.open("Capital.txt");
cout << "Enter the number of countries";
cin >> n;
for (i=0; i < n; i++)
{
cout << "Enter the name of the country ";
cin >> countryarr;
fout1 << countryarr << endl;
cout << "Enter the name of the capital ";
cin >> capitalarr;
fout2 << capitalarr << endl;
}
fout1.close();
fout2.close();
fin1.open("Country.txt"); //Opening the file for
read operations
fin2.open("Capital.txt");
while (fin1&&fin2)
{
if ((fin1.getline(countryarr , size)) ==NULL)
continue;
cout << "Capital of " << countryarr;
fin2.getline(capitalarr , size);
cout << " is " << capitalarr << endl;
}
fin1.close();
fin2.close();
getch();
}
Different modes for opening file
fobj .open(“data.txt”, modes)
1. ios::app  append to the end of file
2. ios:: ate  go to the EOF
3. ios::binary  binary file
4. ios::in  open the file in read only
5. ios::nocreate open fails if file does not exists.
6. ios::noreplace  open fails if the file already exists
7. ios::out  open the file for write only
8. ios::trunc  delete contents of fileif it exists.

Modes can be combined using bit wise or operator |


Eg: fout.open(“data.txt”, ios::app|ios::nocreate)
File pointers
• Each file has two associated pointers.

1. Input pointer or the get pointer


2. Output pointer or the put pointer

These are used to move through the file for


reading or writing. As the reading and writing
takes place the pointer is automatically
incremented.
Default actions
• OPEN THE FILE IN READ ONLY

H E L L O !

I/p ptr

OPEN THE FILE IN APPEND

H E L L O !

O/p ptr

OPEN THE FILE IN WRITE ONLY

O/P prt
Functions for manipulating File pointers
• seekg() : Moves get pointer(input) to a
specified location.
• seekp() : Moves put pointer(output) to a
specified location.
• tellg() : Gives the current position of the get
pointer.
• tellp() : Gives the current position of the put
pointer.
Functions for manipulating File pointers
examples
• fin1.seekg(10);
– This moves the file pointer to the byte number 10 i.e
actually 11th byte as the byte numbers begin from zero.
• ofstream fout1;
fout1.open(“file1.txt”, ios::app);
int p = fout1.tellp();
– On execution of these statements, the output pointer is
moved to the end of the file “file1.txt” and the value of
p will represent the number of bytes in the file.

You might also like