Loading....
Coupon Accepted Successfully!

 

ostrstream

The strstream class is derived from the istrstream and ostrstream classes. The strstream class works with the memory. Using the object of the ostrstream class, different types of data values can be stored in an array.
 

16.23 Write a program to demonstrate use of ostrstreams object.

#include<strstream.h>

#include<iomanip.h>

#include<conio.h>

main()

{

clrscr();

char h=‘C’;
int j=451;
float PI=3.14152;
char txt[]=”applications”;
char buff[70];
ostrstream o (buff,70);
o<<endl <<setw(9)<<“h=”<<h<<endl <<setw(9) <<“j=”<<oct<<j<<endl
<<setw(10)<<“PI=”<<setiosflags(ios::fixed)<<PI<<endl <<setw(11)
<<“txt=”<<txt <<ends;
cout<<o.rdbuf();
return 0;

}

OUTPUT
h=C
j=703
PI=3.14152
txt= applications
 
Explanation: The strstream deals with the memory. If we want to pick characters from an strstream or we want to add characters into the strstream, this can be done by creating istrstream and ostrstream objects. When the object o is created, the constructor of theostrstream is executed. Once an object of the ostrstream is created, we can assign any formatted text to the array associated with it. The statement cout<<o.rdbuf() displays the formatted information on the screen.

istrstream

It is one of the base classes of the strstream class. Using the object of the istrstream class, data can be extracted from an array. Suppose a character array contains numbers and characters. You can extract a number from an array and assign it to an integer variable. Similarly, other values can be extracted from an array. The following program illustrates this:
 

16.24 Write a program to demonstrate the use of istrstream.

#include<strstream.h>

#include<conio.h>

main()

{

clrscr();

char *book;

int pages;
float price;
char *text=”550 175.75 C++”;
istrstream o(text);
o>>pages>>price>>book;
cout<<endl <<pages <<endl <<price<<endl<book;
cout<<o.rdbuf();
return 0;

}

OUTPUT
550
175.75
C++

 

 
Explanation: The istrstreams is the opposite of the strstream. It picks different types of data from an array. In the above program, a character pointer text contains the data of integer, float, and character type. Using the object of the istrstreams class, we can separate the contents and store them in appropriate variables. The *book pointer variable displays a string. The remaining contents are displayed by the function rdbuf().




Test Your Skills Now!
Take a Quiz now
Reviewer Name