4.
Input and Output
Streams
The main () function is defined with the command line arguments as
follows:
int main( int argc, char *argv[])
{
// main program code here
return 0;
}
where argc is the number of arguments passed to the program and
argv [0] are the addresses of the passed arguments. argv[0] is
equivalent to argv[argc-1].
In C++, there are several ways to exit a program:
Call the exit () function.
Call the abort () function.
Execute a return statement from main.
The exit() function, declared in the standard include file STDLIB.H,
terminates a C++ program. The value supplied as an argument to exit is
returned to the operating system as the program’s return code or exit
code. By convention, a return code of zero means that the program
completed successfully.
Issuing a return statement from the main function is equivalent to
calling the exit function with the return value as its argument.
Note that one can use the constants EXIT_FAILURE and EXIT_SUCCESS,
defined in STDLIB.H, to indicate success or failure of the program.
#include <iostream>
using namespace std;
int main()
{
cout <<“Hello, C++ world!\n”;
EXIT_SUCCESS;
}
The abort() function, also declared in the standard include file STDLIB.H,
terminates a C++ program. The difference between exit and abort is
that exit allows the C++ run-time termination processing to take place
(global object destructors will be called), whereas abort terminates the
program immediately.
#include <iostream>
using namespace std;
int main()
{
cout <<“Hello, C++ world!\n”;
abort ();
}
It is advisable not to define macros that have the same names as that of
macros within the standard header.
C++ does not have built-in input and output functions for handling
input and output of data from the outside world, but it supports
different kinds of stream related header files for providing these
facilities.
The Input and Output (I/O) streams fall into two groups, namely, byte
oriented and wide character oriented.
For Conventional Byte Oriented Operations cin, cout, cerr, and clog are
byte oriented, performing conventional byte-at-a-time transfers.
For Handling Wide Characters wcin, wcout, wcerr, and wclog are wide
oriented, translating to and from the wide characters that the program
manipulates internally.
Operations on a stream manipulation are different for byte oriented
and wide character oriented. One cannot perform operations of a
different orientation on the same stream. Therefore, a program cannot
operate interchangeably on both cin and wcin.
Istream, ostream, iostream, wistream, wostream and wiostream.
An output stream object is a destination for bytes. The three most
important output stream classes are ostream, ofstream, and
ostrstream.
An input stream object is a source of bytes. The three most important
input stream classes are istream, ifstream, and istrstream.
The ends is a manipulator used to attach a null terminating character (‘\
0’) at the end of a string.
setw(10);
The setfill() manipulator function is used to specify a different character
to fill the unused field width of the value.
The setprecision() is used to control the number of digits of an output
stream display of a floating point value. The default precision is 6.
Example: cout << setprecision(4) << c << endl;