Module-4-C++
Module-4-C++
Module-4
I/O Streams and File Stream
4.1 I/O Streams
What are C++ streams?
In C++, a stream refers to a sequence of characters that are transferred
between the program and input/output (I/O) devices.
Stream classes in C++ facilitate input and output operations on files and other
I/O devices.
These classes have specific features to handle program input and output,
making it easier to write portable code that can be used across multiple
platforms.
To use streams in C++, you need to include the appropriate header file. For
instance, to use input/output streams, you would include the iostream header
file. This library provides the necessary functions and classes to work with
streams, enabling you to read and write data to and from files and other I/O
devices.
The I/O system of C++ contains a set of classes that define the file handling
methods. These include ifstream, ofstream and fstream. These classes are
derived from fstreambase and from the corresponding iostream class as shown
in Fig. 4.1. These classes, designed to manage the disk files, are declared in
fstream and therefore, we must include this file in any program that uses files.
Fig. 4.1 Stream classes for file operations (contained in fstream file)
Table 4.1 shows the details of file stream classes. Note that these classes contain
many more features.
ifstream opens a file in read mode and ofstream opens it in write mode, whereas
fstream opens it in read–write mode.
The ifstream file is a read-only file, and one can only read from the file defined as
ifstream.
The ofstream file is an output-only file, and one can only write to the file defined
as ofstream.
The fstream file is used for both input and output.
Example Program 1:
//WriteReadText.cpp
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string InputLine, OutputLine;
ofstream EntryFile("FewLines.dat");
cout << "Input :" << endl;
while(true)
{
cin >> InputLine;
if(InputLine == "End")
break;
EntryFile << InputLine << endl; // Writing to EntryFile
}
EntryFile.close();
cout << "Output: " << endl;
ifstream DisplayFile("FewLines.dat");
while(DisplayFile.eof())
{
DisplayFile >> OutputLine;
cout << OutputLine << "\n";
}
DisplayFile.close();
return 0;
}
Input
It was a fight
for pride and ego
for one
It was a fight for
duty and self-respect
for another
who won it at the End
Output
It
was
a
fight
...
who
won
it
at
the
Note* : Using either << or >> will be a problem when a string contains spaces, as it
considers each item separated by a space as an individual item.
Why does not the program display the original lines as they are in the output?
Why are the lines broken into words? It is because the bare ifstream object (such
as cin) is not capable of doing it. The problem of using an ifstream object in its
bare form (i.e., using >>) is that it cannot work with strings containing spaces.
If a string contains spaces, each word is counted as a separate record. One needs
to use either get() and put() (for reading char by char) or use getline() function
for reading lines with spaces.
Example Program 2:
//GetPut.cpp
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#include <iomanip>
int main()
{
char ch;
ofstream EntryFile("FewLines.dat");
while(true)
{
cin.get(ch);
if(ch == '$')
break;
EntryFile << ch;
}
EntryFile.close();
ifstream DisplayFile("FewLines.dat");
while(!DisplayFile.eof())
{
// Do not skip white space
DisplayFile.unsetf(ios::skipws);
DisplayFile >> ch;
cout << ch;
}
DisplayFile.close();
return 0; }
Input
The battle
between One and Another
is
between light and darkness
between truthfulness and falsehood
between duty and ego
$
Output
The battle
between One and Another
is
between light and darkness
between truthfulness and falsehood
between duty and ego
Example Program 3:
//Getline.cpp
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
int main()
{
char InputLine[80], OutputLine[80];
ofstream EntryFile("FewLines.dat");
while(true)
{
cin.getline(InputLine, 80);
if(!strcmp(InputLine, "End")) break;
EntryFile << InputLine << endl;
}
EntryFile.close();
ifstream DisplayFile("FewLines.dat");
while(!DisplayFile.eof())
{
DisplayFile.getline(OutputLine, 80);
cout << OutputLine << endl;
}
DisplayFile.close();
return 0;
}
Input
Imagination is
more important
than knowledge
End
Output
Imagination is more important than knowledge
The only important part of this program involves using getline() function. When
the following statements are executed, the line (maximum 80 characters as
specified in the argument or until the carriage return is encountered) is read into
the input (if cin is used to invoke getline()) or read from the file (if file name is
used to invoke getline()).
cin.getline(InputLine, 80);
DisplayFile.getline(OutputLine, 80);
One can try commenting the fi le generation code from this program, construct a
file using an editor, and see how the program outputs. The program will work
like a DOS-type command and display the contents of the file.
Input
Enter roll no.: 1
Enter name: Bhagath
Enter address: INDIA
Do you want to continue? (y/n): y
Enter roll no.: 2
Enter name: Subash
Enter address: INDIA
Do you want to continue? (y/n): y
Enter roll no.: 3
Enter name: Azad
Enter address: Bharath
Do you want to continue? (y/n): n
if(MCA_StudFile_In.fail())
break;
WriteStudent(MCA_Student_In);
}
MCA_StudFile_In.close();
return 0;
}
Output
The roll no.: 1
The name: Bhagath
The address: INDIA
The roll no.: 2
The name: Subash
The address INDIA
The roll no.: 3
The name: Azad
The address: Bharath