Input Output Formats In C++
On this page (14sections)
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 |
- Home — Browse all Home.
- Polymorphism In C++ — More in Blog.
- Data Hiding and Encapsulation using Access Specifiers in C++ — More in Blog.
- Scope Resolution Operator In C++ — More in Blog.