Home‎ > ‎Blog‎ > ‎

Input Output Formats In C++

posted Dec 6, 2010, 8:44 AM by Thiyagaraaj M   [ updated Jul 18, 2011, 4:53 AM ]

Introduction: 

  • C++ language describes input/output using iostream.h.
  • The standard input / output library for c described by the header stdio.h is still available in C++.
  • The header stream.h was used on systems before release 2.0 and is still available under many C++ systems.

Operator: 

      • <<
      • >>

Output class:
 

  • Output is inserted into an object of type ostream.
  • It is declared in the headerfile iostream.h.
  • An operator << is loaded in this class to perform output conversions from standard types
  • The overloaded left shift operator is called the “insertion or put to operator”.
  • The standard output ostream corresponding to stdout is cout.
  • The standard output ostream corresponding to stderr is cerr.
 

Syntax: 

Cout<<”var”<<var<<”\n”; 

Public members: 

      Ostream& operator<<int(i);

      Ostream& operator<<(long i);

      Ostream& put(char c);

      Ostream& write(const char *p, int n);

      Ostream& flush(); 

The member function flush() forces the stream to be written. 

Formatted output & iomanip.h 

      The put to operator << produces by default the minimum number of characters.

Eg: 

      int i=8,j=9;

      cout<<i<<j;

      cout<<i<<” “j;

      cout<<”i=”<<i<<”j=”<<j;

Immediately prints the line:

          X=1; 

User-defined types: output:
 

    It have typically been printed by creating a member function print().

      Ostream& operator<<(ostream& out,dock x)

      {

      for(int i=0;i<52;++i)

      {

      if(i % 13==0)

      out<<endl;

      out<<x.d[i];

      }

      Return out;


The input class isstream: 

  • An operator >> is overloaded in istream to perform input conversions to standard types
  • The overloaded right shift operator is called the extraction or get from operator.
  • The standard input istream corresponding to stdin is cin.
 

Syntax: 

      Cin>>var>>char; 

Public members: 

      istream& operator>>(int& i);

      istream& get(char& c);

      istream& read(char* s,int n); 

Useful member function: 

      int gcount();

      istream& ignore (int n=1,int delimeter=EOF); 

Files:
 

  • C have stdin,stdout,stderr.
  • In addition, systems may define other standard files such as stdprn & stdaux.
  • File I/O is handled by including fstream.h
  • It contains the classes ofstream and ifstream
 

      Ifstream();

      Ifstream(const char *, int=ios::in);

      Ofstream();

      Ofstream(const char *, int =ios::out); 

Standard files 

CC++NameConnected to
Stdin

Stdout

Stderr

Stdprn

stdaux

Cin

Cout

Cerr

Cprn

caux

Standard input file

Standard ouput file

Standard error file

Standard printer file

Standard auxiliary file

Keyboard

Screen

Screen

Printer

Auxiliary port

 
 

File Modes: 

        ArgumentMode
        ios::in

        ios::app

        ios::out

        ios::noreplace

        Input mode

        Append mode

        Output mode

        If file exists open fails