Module-5
Module-5
Module 5
Streams in C++ :-
We give input to the executing program and the execution program gives back the output. The
sequence of bytes given as input to the executing program and the sequence of bytes that
comes as output from the executing program are called stream. In other words, streams are
nothing but the flow of data in a sequence.
The input and output operation between the executing program and the devices like keyboard
and monitor are known as “console I/O operation”. The input and output operation between the
executing program and files are known as “disk I/O operation”.
In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream available in
fstream headerfile.
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.
ifstream:-
Syntax
ifstream object_name;
object_name.open(“file_name”);
ofstream:-
• This class provides output operations.
• It contains open() function with default output mode.
• Inherits the functions put(), write(), seekp() and tellp() functions from the ostream.
Syntax
ofstream object_name;
object_name.open(“file_name”);
fstream:-
• This class provides support for simultaneous input and output operations.
• Inherits all the functions from istream and ostream classes through iostream.
Syntax
fstream object_name;
bject_name.open(“file_name”, mode);
Modes :
Member Stands
Constant For Access
File open for reading: the internal stream buffer supports input
in * input
operations.
Member Stands
Constant For Access
Binary binary Operations are performed in binary mode rather than text.
Ate at end The output position starts at the end of the file.
ifstream ios::in
ofstream ios::out
There are different types of operations that we can perform on a text file, they are:
The program given below demonstrates how we can create a new text file in memory.
#include <iostream>
#include <fstream>
int main()
fstream fs;
fs.open("e:/data.txt", ios::out);
if(fs.is_open()==0)
fs.close();
return 0;
In the above program, we created fstream object fs and used the open() method to open a file
using the ios::out mode. After that, we used the is_open() method to check if the file opened
successfully or not. This method returns 1 if the file is opened; otherwise, it returns 0 when it
cannot open the file.
The close() method is used to close the file which is opened in the memory.
The program given below demonstrates how we can open an existing text file.
Example
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream fs;
fs.open("e:/data.txt", ios::in);
if(fs.is_open()==0)
{
cout<<"Cannot open file";
}
fs.close();
return 0;
}
In the above program, we have opened the file data.txt for reading using the read mode ios::in.
Write to a file
While doing C++ programming, you write information to a file from your program using the
stream insertion operator (<<) just as you use that operator to output information to the screen.
The only difference is that you use an ofstream or fstream object instead of the cout object.
Let's see the simple example of writing to a text file testout.txt using C++ FileStream
programming.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream filestream("testout.txt");
if (filestream.is_open())
{
filestream << "Dayananda Sagar.\n";
filestream << "CSE Department.\n";
filestream.close();
}
else cout <<"File opening is fail.";
return 0;
}
Output:
Dayananda Sagar.
CSE Department.
You read information from a file into your program using the stream extraction operator (>>)
just as you use that operator to input information from the keyboard. The only difference is that
you use an ifstream or fstream object instead of the cin object.
Let's see the simple example of reading from a text file testout.txt using C++ FileStream
programming.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
string srg;
ifstream filestream("testout.txt");
if (filestream.is_open())
{
while ( getline (filestream,srg) )
{
cout << srg <<endl;
}
filestream.close();
}
else {
cout << "File opening is fail."<<endl;
}
return 0;
}
Output:
Dayananda Sagar.
CSE Department.
#include <iostream>
Department of CSE, DSCE Page 7
Module 5 – I/O Streams
#include <fstream>
using namespace std;
int main () {
char data[100];
// again read the data from the file and display it.
infile >> data;
cout << data << endl;
return 0;
}
When the above code is compiled and executed, it produces the following sample input and
output −
Writing
To write a binary file in C++ use write method. It is used to write a given number of bytes on
the given stream, starting at the position of the "put" pointer. The file is extended if the put
pointer is currently at the end of the file. If this pointer points into the middle of the file,
characters in the file are overwritten with the new data.
If any error has occurred during writing in the file, the stream is placed in an error state.
Syntax of write method
Reading
To read a binary file in C++ use read method. It extracts a given number of bytes from the given
stream and place them into the memory, pointed to by the first parameter. If any error is
occurred during reading in the file, the stream is placed in an error state, all future read operation
will be failed then.
gcount() can be used to count the number of characters has already read. Then clear () can be
used to reset the stream to a usable state.
Syntax of read method
#include<iostream>
#include<fstream>
using namespace std;
struct Student {
int roll_no;
string name;
};
int main() {
ofstream wf("student.dat", ios::out | ios::binary);
if(!wf) {
cout << "Cannot open file!" << endl;
return 1;
}
Student wstu[3];
wstu[0].roll_no = 1;
wstu[0].name = "Ram";
wstu[1].roll_no = 2;
wstu[1].name = "Shyam";
wstu[2].roll_no = 3;
wstu[2].name = "Madhu";
for(int i = 0; i < 3; i++)
wf.write((char *) &wstu[i], sizeof(Student));
wf.close();
if(!wf.good()) {
cout << "Error occurred at writing time!" << endl;
return 1;
}
Output
Student’s Details:
Roll No: 1
Name: Ram
Roll No: 2
Name: Shyam
Roll No: 3
Name: Madhu