0% found this document useful (0 votes)
47 views47 pages

Unit-V File Operations

The document provides an overview of file operations in C++, detailing input and output streams, classes for file stream operations, and various I/O functions like get(), put(), and getline(). It explains file opening modes, how to open and close files, and methods for reading from and writing to files. Additionally, it includes examples of code for practical file manipulation tasks such as copying content between files and counting specific characters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views47 pages

Unit-V File Operations

The document provides an overview of file operations in C++, detailing input and output streams, classes for file stream operations, and various I/O functions like get(), put(), and getline(). It explains file opening modes, how to open and close files, and methods for reading from and writing to files. Additionally, it includes examples of code for practical file manipulation tasks such as copying content between files and counting specific characters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Unit-V

File Operations
Marks-10
Contents:
C++ Streams:
A stream is a sequence of bytes. It acts as a source from which the
input data can be obtained or as a destination to which the input data
can be sent.
Input Stream & Output Stream

►Input Stream
► The source stream that provides data to the program is called input stream.

►Output Stream
► The destination stream that receives output from the program is called output
stream.
Classes for File Stream Operations IO Classes
IO Classes
•iostram
• Contains basic facilities that are used by all other input and output classes.
• It has function to read and write data from stream
•istream
• Inherits the properties of ios.
• Contains overloaded extraction operator >>
• The class provides base for data input from any device.
get(), getline(), read() for reading data
•ostream
• Inherits the properties of ios.
• Contains overloaded insertion operator << to write i. e to insert data into some device.
• function put(), write () to write data to some device.
• Function eof() for checking end of file .
Classes for file stream operations

•filebuf
•It sets the file buffers to read and write.
•fstreambase
•This is the base class for fstream, ifstream and
•ofstream classes.
•ifstream
•It provides input operations for file.
•ofstream
•It provides output operations for file.
•fstream
•It is an input-output stream class
Unformatted I/O Operations:
get():

The class istream defines a member function get() which is used to


handle single character Input operations at a time.
There are two types of get() functions:
Eg.
Syntax:
1)get(char)
cin.get(char);

2)get(void)
char=cin.get();
#include<iostream.h>
/*output
#include<conio.h>
Enter Char: BVIT
#include<fstream.h>
void main() s
{ */
char c;
clrscr();
cout<<"Enter Char:";
cin.get(c);
cout<<c;
getch();
}
#include<iostream.h>
#include<conio.h>

void main()
{
char c;
clrscr();
cout<<"Enter Char:";
c=cin.get();
cout<<c;
getch();
}
put()
The e class ostream defines the member function put() which is used to
output a line of text on a screen character by character.
The function put(),outputs one character at a time.

Syntax:
cout.put(char);
Eg.
cout.put(‘s’);
cout.put(ch);

cout.put(65);
Program:
#include<iostream.h>
#include<conio.h>
/*output
#include<fstream.h> A
void main()
s
{
clrscr();
char c; Enter Char:h
cout.put(65)<<endl;
cout.put('s')<<endl;
h
cout<<"\nEnter Char:"; */
cin.get(c);
cout.put(c);
getch();
}
getline()
The getline() function reads a whole line of text ending with a newline
character. This function reads the newline character but it does not
save it.

Syntax:
Cin.getline(line,size);
#include<iostream.h>
#include<conio.h>
/*output
#include<fstream.h> Enter Line:object oriented
void main() programming is fun
{ object oriented pro
int n=20;
char c[20]; */
clrscr();
cout<<"Enter Line:";
cin.getline(c,n);
cout<<c;
getch();
}
write()
The write() is a member function of ostream class it is used to display
entire line on the screen.

Syntax:
cout.write(line,size);
#include<iostream.h>
#include<conio.h>
void main()
/*output
{ Enter the Line:object oriented
clrscr(); programming is fun
int n=20; object oriented
char c[20]; */
cout<<"Enter the Line:";
cin.getline(c,n);
cout.write(c,16);
getch();
}
Concept Map
File opening modes

► In C++, for every file operation, exists a specific file mode.


► These file modes allow us to create, read, write, append or modify a file.
► The file modes are defined in the class ios.
► A File stream act as an interface between the program and the files.

Parameter Meaning
ios :: app Append to end-of-file
ios :: ate Go to end-of-file on opening
ios :: binary Binary file
ios :: in Open file for read only
ios :: nocreate Open fails if file does not exist
ios :: noreplace Open fails if file does already exist
ios :: out Open file for writing only
ios :: trunc Delete the contents of the file if it exists

Page 24 Maharashtra State Board of Technical Education 4 July 2020


Default opening modes of a file

► For ifstream ----- ios::in


► For ofstream ------ ios::out
► For fstream --------- ios::in | ios::out

Page 25 Maharashtra State Board of Technical Education 4 July 2020


Opening a File

► A file must be opened before reading from it or writing into it.


► Either ofstream or fstream object may be used to open a file for writing.
► And ifstream object is used to open a file for reading purpose only.
► Syntax to open a file:
1. file_stream_object.open(“filename”,mode);
2. file_stream_class file_stream_object(“filename”,mode);
23222
where,

❑ file_stream-object: is the an object of a file stream class used to perform a specific file operation.
❑ file_stream-class: is ofstream is used to create o/p stream and ifstream is used to create i/p stream.
❑ filename: is the name of a file on which we are going to perform file operations.
❑ mode: is single or multiple file modes in which we are going to open a file.

Page 26 Maharashtra State Board of Technical Education 4 July 2020


Closing a File

► When a C++ program terminates it automatically flushes all the streams, release all the allocated
memory and close all the opened files.
► But it is always a good practice that a programmer should close all the opened files before program
termination.
► Syntax to close a file:

file_stream_object.close();
where,

❑ file_stream-object: is the an object of a file stream class used to perform a specific file operation.

Page 27 Maharashtra State Board of Technical Education 4 July 2020


/*WAP to opening a file and closing a file */
#include<iostream.h>
#include<conio.h>
#include<fstream.h>

void main()
{
clrscr();
fstream f;
f.open("shakir.cpp",ios::in);
cout<<"File opened"<<endl;
f.close();
cout<<"File closed";
getch();
}
/*WAP to opening a file and reading the data from that file and closing a file */

#include<iostream.h> while(f)
{
#include<conio.h> f.getline(line,100);
#include<fstream.h> cout<<line<<endl;
}
f.close();
void main() cout<<"File closed";
{ getch();
}
clrscr(); /*Output:
fstream f; File opened
Opening a file
char line[100];
f.open(“file.cpp",ios::in); File closed
cout<<"File opened"<<endl; */
Writing into a File

► While programming in C++, the information to be written into a file uses the stream insertion operator
(<<) operator to output information to the screen.
► The only difference is that you use an ofstream or fstream object instead of the cout object.

Page 30 Maharashtra State Board of Technical Education 4 July 2020


Writing data into file using open
function
#include<iostream.h> /* Output:
#include<conio.h>
Writting into the File
#include<fstream.h>
void main() */
{
clrscr();
ofstream outfile;
outfile.open("w1.cpp",ios::out);
cout<<"Writting into the File"<<endl;
outfile<<“Welcome to CPP";
outfile.close();
getch();
}
Writing data into file using
constructor
#include<iostream.h> /* Output:
#include<conio.h>
Writting into the File
#include<fstream.h>
void main()
*/
{
clrscr();
ofstream outfile("w1.cpp",ios::out);
cout<<"Writting into the File"<<endl;
outfile<<" Welcome in to BVIT";
outfile.close();
getch();
}
#include<iostream.h> cout<<"Writting into the
#include<conio.h> File"<<endl;
#include<fstream.h> f<<“Today is holiday";
void main() f.close();
{ getch();
clrscr(); }
fstream f; /* Output:
f.open(“Tanu.cpp",ios::out); Writting into the File
*/
Reading from a File

► The information can be read from a file into another program using the stream extraction operator
(>>)
► The only difference is that you use an ifstream or fstream object instead of the cin object.

Page 34 Maharashtra State Board of Technical Education 4 July 2020


#include<iostream.h> while(f)
#include<conio.h> {
#include<fstream.h> f.getline(line,100);
cout<<line<<endl;
void main()
}
{
f.close();
clrscr();
getch();
fstream f;
char line[100]; }
f.open("fun.cpp",ios::in);
cout<<"Content of files are:"<<endl;
Reading from file using constructor
#include<iostream.h> while(infile)
#include<conio.h> {
#include<fstream.h> infile.getline(line,100);
void main() cout<<line;
{ }
clrscr(); infile.close();
char line[100]; getch();
ifstream infile("w1.cpp",ios::in); }
cout<<"Reading fron File"<<endl;
#include<iostream.h> ifstream infile("w1.cpp",ios::in);
#include<conio.h> infile>>name;
#include<fstream.h> cout<<endl;
void main() cout<<"name:"<<name;
{ infile.close();
clrscr(); getch();
}
ofstream outfile("w1.cpp",ios::out); /* Output:
cout<<"Writting into the File"<<endl; Writting into the File
cout<<"Enter data:"; Enter data:OOP
char name[100];
cin>>name;
outfile<<name<<endl; name:OOP
outfile.close(); */
Detecting End of File

► Method 1:

► Detection of the end-of-file condition is necessary for preventing any further attempt to read data
from the file.
while(fin)
► An ifstream object returns a value zero if any error occurs in the file operation including the end-of-file
condition.

Page 38 Maharashtra State Board of Technical Education 7 July 2020


Detecting End of File

► Method 2:

if(fin1.eof() != 0 )
{
exit(1);
}
► The eof() of ios class returns a non zero value if the end-of-file condition is encountered and zero
otherwise.

Page 39 Maharashtra State Board of Technical Education 7 July 2020


Concept Map

File
manipulations

Copy content Count no of


Count no of Count no of
from one file specific
lines spaces
to another character

Page 40 Maharashtra State Board of Technical Education 7 July 2020


WAP to copy content of one file into another
using open function
#include<iostream.h> while(infile)
#include<conio.h>
{
#include<fstream.h>
infile.get(ch);
void main() cout<<ch;
{ outfile.put(ch);
clrscr();
ifstream infile;
}
ofstream outfile; infile.close();
char ch; outfile.close();
infile.open(“Tanu.cpp",ios::in);
getch();
outfile.open(“Ram.cpp",ios::out);
cout<<"copied content"<<endl; }
WAP to copy content of one file into another using constructor

#include<iostream.h> while(infile)
#include<conio.h> {
#include<fstream.h> infile.get(ch);
cout<<ch;
void main()
outfile.put(ch);
{
}
clrscr();
infile.close();
char ch;
ifstream infile(“Ira.cpp"); outfile.close();
ofstream outfile("gauri.cpp"); getch();
cout<<"copied content"<<endl; }
#include<iostream.h>
#include<conio.h> else if(c=='\n')
#include<fstream.h> nol++;
void main() }
{
cout<<"No. of Lines:"<<nol<<endl;
int nob=0,not=0,nol=0,cnt=0;
clrscr(); cout<<"No. of Blank:"<<nob<<endl;
char c; cout<<"No. of Tab"<<not<<endl;
ifstream infile; getch();
infile.open("virtual.cpp");
while(!infile.eof())
}
{ /* Output:
infile.get(c); No. of Lines:28
cnt++;
No. of Blank:11
if(c==' ')
nob++;
else if(c=='\t')
No. of Tab0
not++; */
#include<iostream.h> while(!infile.eof())
#include<conio.h> {
#include<fstream.h> infile.get(c);
if(c==choice)
void main()
cnt++;
{
}
int cnt=0;
cout<<"No. of characters:"<<cnt<<endl;
clrscr(); getch();
char c,choice; }
cout<<"Enter character tobe /* Output:
counted:"<<endl; Enter character tobe counted:
cin>>choice; s
ifstream infile; No. of characters:18
infile.open("virtual.cpp"); */
outfile.open("item3.cpp");
#include<iostream.h> outfile<<"External"<<endl;
#include<fstream.h> outfile<<"Internal"<<endl;
#include<conio.h> outfile.close();
void main() const int n=40;
char line[n];
{
ifstream infile;
clrscr(); infile.open("item2.cpp");
ofstream outfile; cout<<endl;
outfile.open("item2.cpp"); while(infile)
{
outfile<<"OOP"<<endl;
infile.getline(line,n);
outfile<<"RDBMS"<<endl; cout<<line<<endl;
outfile.close(); }
infile.close(); /* Output:
infile.open("item3.cpp");
OOP
cout<<endl;
while(infile) RDBMS
{
infile.getline(line,n);
cout<<line<<endl;
}
infile.close(); External

getch(); Internal
} */
#include<iostream.h> while(inf)
#include<conio.h> {
#include<fstream.h> inf.get(data);
void main() cout<<data;
{ outf.put(data);
}
char s[20],d[20],data;
inf.close();
clrscr();
outf.close();
cout<<"Enter Source File name:";
getch();
cin>>s;
}
cout<<"Enter Destination File Name:"; /* Output:
cin>>d; Enter Source File name:w1.cpp
ifstream inf; Enter Destination File Name:w2.cpp
inf.open(s);
ofstream outf; OOP
outf.open(d); */

You might also like