• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

IO Streams in C++

I/O streams

C++ uses the concept of stream and stream classes to implement I/O operations with console and disk files

The input functions are defined in istream class

The output functions are defined in ostream class.

The IOstream class inherits the properties of istream and ostream classes.

Stream is a class which provides set of functions to perform input and output operations

Stream represents input source and output destination

 

 

Screenshot_34

 

Output Stream in C++

Output stream class provides output functions

 

Put()

Member function of ostream class, cout.put(ch);//

displays the value of variable ch.

  • It can also be used to output a line of text, character by character.

 

Write()

Member function of ostream class, write() function displays the entire line cout. write(linesize);

Line represents the name of the string to be displayed, size indicates the no.of characters to display.

It doesn’t stop displaying the characters when null character is encountered.

Example of Output Stream in C++

# include<iostream.h>

void main()

{

cout.put(‘A’);  // we can use only one character

cout<<’A’;

cout<<10;

cout<<1.5;

cout.write(“Hello C++”,q);

cout.write(“Hello c++”,b);

cout<<”Hello C++”;

}

 

 

Format functions

Width() to specify the required field size for displaying an output.

cout.width(w);

The output will be printed in the field of w characters wide at the right end of the field.

Example of Format Functions

int a=10;(2 width)

printf(“%4d”,a); //(2 for space)

precision():

By default, the floating numbers are printed with six digits after the point, We can specify the number of digits to be displayed after the decimal point.

cout.precision(d);

sets d number of digits to the right of decimal pair.

  

Example

# include<iostream.h>

void main()

{

int a=10;

cout.width(5);

cout<<a<<endl;

float b=1.234;

cout.precission(2);

cout<<b<<endl;

}

o/p : – – – 10

1.23

Note : Numbers are right aligned.

FILL ()

The unused position of the field are filled with white spaces, by default we can fill them by the designs character by using fill() function

cout.fill(ch);

ch is used to fill the unused positions

{

int a=10;

cout.width(5);

cout.fill(‘*’);

cout<<a<<endl;

}

Setf() :

cout .setf(ios::show point) displays tralling zeros

cout.setf (ios:: show pos) displays plus sign before a positive number

 

Example

# include <iostream.h>

# include<conio.h>

void main(){

flaot a=10;

cout.setf(ios::showpoint);

cout<<a<<endl;

int b=20;

cout.setf(ios:: showpos);

cout<<b<<endl;

}

O/p:

10.000000

-20

Istream class in C++

This class provide member functions to performs input operations.

get():

Member function of istream class.

Fetches a single character including a blank space,tab and new line characters, get() can be used in 2 ways;

cin .get(ch);//get a character from keyboard and assigns it to ch.

ch=cin.get(); also can be used

getline():

Member function of istream class.

We can read the text ….

oriented getline()

cin.getline(line,size);

Read character input into the variable line The reading is terminated as soon as either ‘\n’ is encountered of size characters are read.

 

Example

# include <iostream.h>

# include <conio.h>

void main()

{

char anem[10];

clrscr();

cout<<”\n Input name’:

cin .get line(name,10);

cout<<name;

}

Example

# include <iostream.h>

#include <conio.h>

void main()

{

char ch;

clrscr();

cout<<”\n Input any character including space”;

ch=cin.get();

cout<<endl<<ch;

}

 

Summary
Review Date
Reviewed Item
IO Streams in C++
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on IO Streams in C++"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.