This document discusses C++ stream classes and file input/output. It covers the key stream classes like iostream, istream, ostream and their functions. It also discusses file classes like ifstream, ofstream and fstream that are used for file input/output. It provides examples of reading from and writing to files using these classes and their functions like open(), get(), put() etc. It mentions how file pointers are used to manipulate file positions for input/output operations.
3. Ios:
It contains basic facilities that are used by all other
input and output classes.
Istream:
It inherites the properties of ios and it declares input
functions such as get(),getline() and read().
Ostream:
It inherites the properties of ios and it declares
output functions such as put() and write().
Iostream:
It inherites the properties of ios stream and ostream.
Streambuf:
It provides an interface to physical devices through
buffers.
4. The features that supports to format the console
I/O operations are:
ā¦ Ios class functions and flags
ā¦ Manipulators
ā¦ User-defined output functions
IOS CLASS FUNCTIONS AND FLAGS:
It consists of large number of member functions
that is used to format the output in number of ways.
5. ļ½ MANIPULATORS:
These are the special functions that are
included in the I/O statements to format the
parameters of a stream.
ļ½ DEFINING FIELD WIDTH:
ā¦ It is used to define the width of the field.
ā¦ Can be defined using width();
ļSETTING PRECISION:
It is used to specify the to be displayed after the
decimal point.
ļ¶FILLING AND PADDING:
ļ§ It is used to print the values using the larger field
widths.
ļ§ It can be declared by,
cout.fill();
6. #include <iostream>
using namespace std;
int main()
{
int item[4] ={ 10,8,12,15};
int cost[4]={75,100,60,99};
cout.width(5);
cout<<āItemsā;
cout.width(8);
cout<<āCostā;
10. It is used for defining the input and output in
various forms.
Overloaded Operators >> and <<:
ā¢ It is used to give the I/O
ā¢ The >> is overloaded in istream class
ā¢ The << is overloaded in ostream class
Ex:
cin>>item1>>item2;
11. put() and get() Functions:
ā¦ It is used for the input and output .
ā¦ Put(c) is used to give the input
ā¦ Get(c) is used to get the output
EXAMPLE:
#include <iostream>
using namespace std;
int main()
{
int count=0;
char c;
cout<<āINPUT TEXT nā;
cin.get( c );
while ( c 1=ānā )
12. while ( c 1=ānā )
{
cout.put( c);
count++;
cin.get( c );
}
cout<< ān Number of characters =ā <<count <<ānā;
return 0;
}
OUTPUT:
Object oriented programming
Number of characters=27
13. Getline() and write() Functions:
ā¢ The getline() function reads a whole line of the
text and ends with a newline character.
ā¢ This function can be invoked by,
cin.getline (line,size);
ā¢ The writeline() function reads a whole line of the
text and displays an entire line.
ā¢ This function can be invoked by,
cout.write (line,size);
14. EXAMPLE:
#include <iostream>
using namespace std;
int main()
{
int size=20;
char city[20];
cout<<āenter city name:n ā;
cin>>city;
cout<<ācity name:ā<<city<<ānnā;
cout<<āenter city name again: nā;
cin.getline(city,size);
16. OUTPUT:
first run
Enter city name:
Delhi
Enter city name again:
City name now:
Enter another city name:
Chennai
New city name:
Chennai
17. ļ½ The header file iomanip provides a set of functions
called manipulators which can be used to manipulate
the output formats.
ļ½ They provide the same features as that of the ios
member function and flag.
ļ½ Two or more manipulators can be,
Cout<<manip1<<manip1<<manip<<item;
Cout<<manip1<<item1<<manip2<<item2;
20. ļ½ The large amount of Data can be handled using some
devices such as floppy disk or hard disk to store those
datas.
ļ½ These datas are stored in these devices called FILES.
ļ½ The Programs can be designed to perform the read and
write operations on those files.
ļ½ Kinds of Data Communication:
ā¦ Data Transfer between the console unit and the
program.
ā¦ Data Transfer between the program and a Diskfile.
21.
22. FILEBUF:
It is used to set the file buffers to read and write.
FSTREAMBASE:
It provides operations common to the file streams.
IFSTREAM:
It provides input operations.
OFSTREAM:
It provides output operations.
FSTREAM:
It provides support for both input and output
operations.
23. ļ½ The I/O system contains a set of classes that define
the file handling methods.
ļ½ These includes ifstream, ofstream and fstream.
ļ½ These classes are derived from fstreambase and from
the corresponding iostream class.
ļ½ These classes are designed to manage the Disk files,
which are declared in fstream and we should include
any program in these files.
24. ļ A file stream can be declared using the classes
ifstream, ofstream and fstream that are contained in
the Header file fstream.
ļ A file can be Opened into two ways:
ļUsing the Constructor function of the class
ļUsing the Member function open() of the class
OPENING FILES USING Open():
ļ The function open() can be used to open multiple
files that use the same stream object.
file-stream-class stream-object;
stream-object.open (āfile nameā);
27. This condition is necessary for preventing data
from the file. This condition is
MORE ABOUT OPEN( ): FILE MODES
ā¢ The ifstream and ofstream is used to create
the new files.
ā¢ The function open( ) is used to open a new
stream.
object. open(āfile nameā,mode);
28. Each file has two associated pointers known
as File pointers.
DEFAULT ACTIONS:
When we open a file in read-only-mode
,these input pointer is automatically set at the
beginning.
FUNCTIONS FOR MANIPULATION OF FILE POINTERS:
ļ§ seekg()-Moves a pointer to specified location
ļ§ seekp()-Moves put pointer to a specified location
ļ§ tellg()-Gives the current position of the get pointer
ļ§ tellp()-Gives the current position of the put pointer
29. It supports a number of member function for
performing the input/output operations on files.
Put() and get() Functions:
ā¦ It is used to handle single character at a time.
ā¦ The function put() writes a single character to the
associated stream.
ā¦ The function get() reads a single character to the
associated stream.
Write() and read() Functions:
It is used to read and write the blocks in the binary
data.
30. ļ½ It is used to maintain the routine task in the data file.
ļ½ The updation can be done in:
ā¦ Displaying the contents of a file
ā¦ Modifying an existing item
ā¦ Adding a new file
ā¦ Deleting an existing file
31. ļ½ The reading and writing files may not be true
always.
ā¦ A file which we are attempting to open for reading
does not exist.
ā¦ The file name used for a new file may already exist.
ā¦ We may use an invalid file name.
ā¦ There may not be any space in the disk for storing
more data.
ā¦ We may attempt to perform an operation when the
file is not opened for that purpose.
32. ļ½ This feature facilities the supply of arguments to the
main() function.
ļ½ These arguments are supplied at the time of invoking
the program.
ā¦ C> exam data results
ā¦ Exam is the name of the file containing the program
to be executed ,data and results are the filenames
passed to the program as command-line arguments.