Chapter 16 introduced the concepts and syntax for object-oriented classes, constructors, destructors, member functions, and operators. This knowledge is required for a deeper understanding of C++ I/O.
C++, like C, does not have any built-in I/O routines. Instead, all C++ compilers come bundled with object-oriented iostream classes. These standard I/O class objects have a cross-compiler syntax consistency because they were developed by the authors of the C++ language. If you are trying to write a C++ application that is portable to other C++ compilers, you will want to use these iostream classes. The Visual C++ compiler provides the following five ways to perform C++ I/O:
•
ANSI C buffered I/O C also supports buffered functions such as fread( ) and fwrite( ). These STDIO.H library functions perform their own buffering before calling the direct I/O base routines.
•
C console and port I/O C provides additional I/O routines that have no C++ equivalent, such as _getch( ), _ungetch( ), and _kbhit( ). All non-Windows applications can use these functions, which give direct access to the hardware.
•
Microsoft Foundation Class library The Microsoft CFile class found in the Foundation Class library provides C++, and especially Windows applications, with objects for disk I/O. Using this library of routines guarantees that your application will be portable and easy to maintain.
•
Microsoft iostream class library The iostream class library provides C++ programs with object-oriented I/O. This I/O can be used in place of functions such as scanf( ), printf( ), fscanf( ), and fprintf( ). However, while these iostream classes are not required by C++ programs, many of the character-mode objects, such as cin, cout, cerr, and clog, are incompatible with the Windows graphical user interface.
•
Unbuffered C library I/O The C compiler provides unbuffered I/O through functions such as _read( ) and _write( ). These functions are very popular with C programmers because of their efficiency and the ease with which they can be customized.