Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C Day-14

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 13

Session Objectives

Explain Files & its Basics

File modes

Writing data to a file & reading data from a file

Explain File copy


 A Stream is generally referred to flow of data.

 Different streams are used to represent different kinds of data flow

 Each stream is associated with particular class which contains


member functions and definitions for dealing with that particular kind
of data flow

 The sequence of input bytes are called as Input Stream

 The sequence of output bytes are called as Output Stream

 The cin object with >>operator is used for getting input

 The cout object with <<operator is used for displaying output

 In C++, the number of base and derived classes are available in


Stream I/O classes
ios

istream ostream

iostream

ifstream fstream ofstream


The Header File <fstream.h> contains the definitions for file stream
classes
It contain the definitions for three classes. They are
1) ifstream
2) ofstream
3) fstream
Ofstream :
Every object of type ofstream uses a filename to store
information in that file.
Ifstream :
To read the contents from a file we must create an object of
class ifstream.
Opening a File
The Open() function is used to open different files that uses the
same stream object.
Syntax : open(“filename”, file mode);

File Mode Description


ios :: in Open file for reading
ios :: out Open file for writing
ios :: app Open file for reading and/or writing the data at
the end of file (append)
ios :: ate Erase the file before reading or
writing(Truncated)
ios :: nocreate Error when opening, if file does not already
exists
ios :: no replace Error when opening, if file does not already
exists, unless ate or app is set
ios :: binary Open file in binary mode (not text mode)
Closing a File
To close a file, the member function close() must be used. It takes
no parameters and returns no value.
Syntax : close();

Detecting End-Of-File
It is necessary for preventing any attempt to read data from the
file.
Syntax : eof()

put() and get() functions :


put() function displays a single character on the screen.
Syntax : put(char);
get() function gets a single character from the file
Syntax : get(void)
File creation(Writing the contents or data to a file )
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main() l t:
{ esu l ly
R sf u
ofstream outfile; ces ed
Su reat
char fname[20]; C
cout<<"enter the filename"<<endl;
cin>>fname;
outfile.open(fname);
outfile<<"Sachin"<<endl;
outfile<<"Jackie"<<endl;
outfile<<"Vijay"<<endl;
cout<<"\n "<<"Sucessfully created ";
outfile.close();
}
Read the contents(data) from the file
#include<iostream.h> while(!infile.eof())
#include<conio.h> {
#include<stdlib.h> ch=infile.get();
#include<fstream.h> cout<<ch;
void main() }
{ infile.close();
ifstream infile; }
char fname[20];
char ch;
cout<<"enter the filename ";
cin>>fname;
infile.open(fname);
if(infile.fail())
{
cout<<"file does not exist";
exit(1);
}
copy the contents from one file into another file
#include<iostream.h> if(outfile.fail())
#include<conio.h> {
#include<stdlib.h> cout<<"unable to create a file";
#include<fstream.h> exit(1);
void main() }
{ while(!infile.eof())
ofstream outfile; {
ifstream infile; ch=infile.get();
char source[10],target[10],ch ; outfile.put(ch);
clrscr(); cout<<ch;
cout<<"enter the source file name"; }
cin>>source; infile.close();
cout<<"enter the target file name"; outfile.close();
cin>>target; getch();
infile.open(source); }
if(infile.fail())
{
cout<<"source file does not exist";
exit(1);
}
outfile.open(target);
The getline() and write() functions
These two functions are line oriented input and output functions
respectively.

getline() function reads a complete line until it encounters a


newline character.

Syntax : cin.getline(char*,size);

Char*  holds the inputline

Size  The number of characters to be read

write Function
It is used for displaying a text in a line until it encounters a ‘\n’
characters.

Syntax : cout.write(char*,size);
getline() and write() function Example

#include<iostream.h>
#include<conio.h>
void main()
{
char name[50];
clrscr();
Cout<<“Enetr any string”<<endl;
cin.getline(name,40);
cout.write(name,40);
getch();
}

Result
Enter any String : India is My Country

India is My Country
 A stream is generally referred to a flow of data.
 Each stream is associated with a particular class which contains
member functions and definitions for dealing with that particular
kind of data flow
 The sequence of input bytes are called Input stream
 The sequence of output bytes are called as output stream
 The Ofstream is used for writing information to a file
 The ifstream class is used for reading information from a file
 The stream class is used for both writing and reading from a file

You might also like