Input Output Formats In C++
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.
Input and Output Operator:
- << - for Printing Variables
- >> - for Reading Variables
Output class:
- The output is inserted into an object of type ostream.
- It is declared in the header file 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.
Output cout 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.
Example:
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 has 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 the operator.
- The standard input istream corresponding to stdin is cin.
Input 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();
stream& ignore (int n=1,int delimeter=EOF);
Files Input and Output
- 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 format:
C | C++ | Name | Connected 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 |
Standard File Modes:
Argument | Mode |
ios::in
ios::app ios::out ios::noreplace |
Input mode
Append mode Output mode If file exists open fails |
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Example Program For Copy Constructor In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Simple Addition ( Add Two Integers ) Example Program
- Factorial Using Function Example Program In C++
- Simple Example Program For Constructor In C++
- Simple Program for Read user Input Using cin
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Binary Operator Overloading Using C++ Programming
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++