Module_4_IO Streams and Files_Lecture Notes (1)
Module_4_IO Streams and Files_Lecture Notes (1)
C++ STREAMS
A stream is a sequence of bytes. It acts either as a source from which the input data can be obtained or as a
destination to which the output data can be sent. The source stream that provides data to the program is called
the input stream and the destination stream that receives output from the program is called the output stream. A
program extracts the bytes from an input stream and inserts bytes into an output stream.
ios is the base class for istream (input stream) and ostream (output stream) which are base classes for iostream
(input/output stream). The class ios is declared as the virtual base class so that only one copy of its members
are inherited by the iostream.The class ios provides support for formatted and unformatted I/O operations.
Class istream is overloaded with >> operator. Class ifstream is derived from istream, this class
provides input operations. It is used to read from files. It contains open ( ) function with default input mode.
Inherits the function get ( ), getline ( ), read ( ), seekg ( ) and tellg ( ) functions from istream.
Class ostream is overloaded with << operator. Class ofstream is derived from ostream, this class
provides output operations. It is used to write to files. It contains open ( ) function with default output mode.
Inherits the function put ( ), write ( ), seekp ( ), tellp ( ) functions from ostream.
Class fstream is stream class used to read from and write to files.
MODULE 4: I/O Streams and Files
ifstream ifstream_object;
ifstream_object.open (“fewlines.dat”); // opening a file to read from the file
MODULE 4: I/O Streams and Files
int main()
{
Students S;
int n;
cout<<"Enter no. of student: ";
cin>>n;
ofstream save("Data.txt");
if(!save)
{
cout<<"\nFile not found!!";
return 0;
}
for(int i=1;i<=n;i++)
{
cout<<"Details of the student:-"<<endl;
cout<<"Name: ";
cin>>S.name;
cout<<"Marks: ";
cin>>S.marks;
save<<"Details of the student:-"<<endl;
save<<"Name: "<<S.name<<endl;
save<<"Marks: "<<S.marks<<endl<<endl;
}
string str;
MODULE 4: I/O Streams and Files
ifstream in("Data.txt");
cout<<"\nData stored in the file:-"<<endl;
while(getline(in,str))
{
cout<<str<<endl;
}
return 0;
}
One needs to use either get() and put() (for reading char by char) or use getline() function for reading lines with
spaces.
Using getline ( )
We can even use cout and cin with getline() instead of get(). The same getline() available with cin is also
available to all istream objects. It is also available to the DisplayFile().
Program: Usage of getline( )
//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
MODULE 4: I/O Streams and Files
Working of the program:
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);
BINARY FILES
These files are more useful for storing structures of information.
Opening a Binary File
We can open a binary file using constructor and open( ) member function.
A binary file can be opened using a constructor. The constructors for ofstream and ifstream are acceptable for
text files. For binary files, another constructor with two arguments is needed. The first argument is the name of
the file and the second one is the file mode.
// Using open methods
ofstream BE_StudFile_Out;
BE_StudFile_Out.open("BE.dat", ios::out | ios::binary | ios::trunc);
// Using constructor
ofstream BE_StudFile_Out("BE.dat", ios::in | ios::binary | ios::trunc);
The first example is using the open method and the second one is using a constructor. Programmers, therefore,
usually use the second method.
The same effect can be achieved by writing either of the following:
ofstream BE_StudFile_Out("BE.dat", ios::out | ios::binary | ios::trunc); //constructor
Mode ios::out means it is a file that is opened for the purpose of writing, ios::binary means there is no
translation that will take place while we write and with the output file there is another parameter ios::trunc
which means that if the file that we are trying to open and write the contents is already present the previous
contents are deleted so that we can write the new contents.
ifstream BE_StudFile_In;
BE_StudFile_In.open("BE.dat", ios::in | ios::binary); //open member function
}
};
int main()
{
timeVal writeObj, readObj;
MODULE 4: I/O Streams and Files
int hh, mm, ss;
char ampm[3];
cout << "Enter Hours : ";
cin >> hh;
cout << "Enter Minutes : ";
cin >> mm;
cout << "Enter Seconds : ";
cin >> ss;
cout << "Enter am or pm : ";
cin >> ampm;
writeObj.setdata(hh,mm,ss,ampm);
ofstream outFile("TimeFile", ios::out | ios::binary);
if(!outFile)
{
cout << "Cannot open file.\n";
return 1;
}
outFile.write((char *) &writeObj, sizeof(timeVal));
cout << "\nWritten the time object successfully to binary file" << endl;
outFile.close();
readObj.showdata();
inFile.close();
return 0;
}