Which of the following is the member function that reads a single character from a file?

The C language did not build the input/output facilities into the language. In other words, there is no keyword like read or write. Instead, it left the IO to the compiler as external library functions [such as printf and scanf in stdio library]. The ANSI C standard formalized these IO functions into Standard IO package [stdio.h]. C++ continues this approach and formalizes IO in libraries such as iostream and fstream.

Features
  • C++ IO is type safe. IO operations are defined for each of the type. If IO operations are not defined for a particular type, compiler will generate an error.
  • C++ IO operations are based on streams of bytes and are device independent. The same set of operations can be applied to different types of IO devices.

Stream IO

Streams

C/C++ IO are based on streams, which are sequence of bytes flowing in and out of the programs [just like water and oil flowing through a pipe]. In input operations, data bytes flow from an input source [such as keyboard, file, network or another program] into the program. In output operations, data bytes flow from the program to an output sink [such as console, file, network or another program]. Streams acts as an intermediaries between the programs and the actual IO devices, in such the way that frees the programmers from handling the actual devices, so as to archive device independent IO operations.

C++ provides both the formatted and unformatted IO functions. In formatted or high-level IO, bytes are grouped and converted to types such as int, double, string or user-defined types. In unformatted or low-level IO, bytes are treated as raw bytes and unconverted. Formatted IO operations are supported via overloading the stream insertion [] operators, which presents a consistent public IO interface.

To perform input and output, a C++ program:

  1. Construct a stream object.
  2. Connect [Associate] the stream object to an actual IO device [e.g., keyboard, console, file, network, another program].
  3. Perform input/output operations on the stream, via the functions defined in the stream's pubic interface in a device independent manner. Some functions convert the data between the external format and internal format [formatted IO]; while other does not [unformatted or binary IO].
  4. Disconnect [Dissociate] the stream to the actual IO device [e.g., close the file].
  5. Free the stream object.

C++ IO Headers, Templates and Classes

Headers

C++ IO is provided in headers [which included , , and ], [for file IO], and [for string IO]. Furthermore, the header provided manipulators such as setw[], setprecision[]setfill[] and setbase[] for formatting.

Template Classes

In order to support various character sets [char and wchar_t in C++98/03; and char16_t, char32_t introduced in C++11], the stream classes are written as template classes, which could be instantiated with an actual character type. Most of the template classes take two type parameters. For example,

template 
class basic_istream;
 
template 
class basic_ostream;

where:

  • charT is the character type, such as char or wchar_t;
  • traits, of another template class char_traits, defined the properties of the character operations such as the collating sequence [sorting order] of character set.
Template Instantiations and typedef

As mention, the basic_xxx template classes can be instantiated with a character type, such as char and wchar_t. C++ further provides typedef statements to name these classes:

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
Specialization Classes for char type

We shall focus on the specialization classes for char type:

  • ios_base and ios: superclasses to maintain common stream properties such as format flag, field width, precision and locale. The superclass ios_base [which is not a template class] maintains data that is independent of the template parameters; whereas the subclass ios [instantiation of template basic_ios] maintains data which is dependent of the template parameters.
  • istream [basic_istream], ostream [basic_ostream]: provide the input and output public interfaces.
  • iostream [basic_iostream]: subclass of both istream and ostream, which supports bidirectional input and output operations. Take note that istream and ostream are unidirectional streams; whereas iostream is bidirectional. basic_iostream template and iostream class is declared in the header, not header.
  • ifstream, ofstream and fstream: for file input, output and bidirectional input/output.
  • istringstream, ostringstream and stringstream: for string buffer input, output and bidirectional input/output.
  • streambuf, filebuf and stringbuf: provide memory buffer for the stream, file-stream and string-stream, and the public interface for accessing and managing the buffer.

Buffered IO

[TODO]

The Header and the Standard Stream Objects: cin, cout, cerr and clog

The header also included the these headers: , , and . Hence, your program needs to include only the header for IO operations.

The header declares these standard stream objects:

  1. cin [of istream class, basic_istream specialization], wcin [of wistream class, basic_istream specialization]: corresponding to the standard input stream, defaulted to keyword.
  2. cout [of ostream class], wcout [of wostream class]: corresponding to the standard output stream, defaulted to the display console.
  3. cerr [of ostream class], wcerr [of wostream class]: corresponding to the standard error stream, defaulted to the display console.
  4. clog [of ostream class], wclog [of wostream class]: corresponding to the standard log stream, defaulted to the display console.

The Stream Insertion Operators

Formatted output is carried out on streams via the stream insertion operators. For example,

cout > variable;

Take note that cin/cout shall be the left operand and the data flow in the direction of the arrows.

The operators are overloaded to handle fundamental types [such as int and double], and classes [such as string]. You can also overload these operators for your own user-defined types.

The cin return a reference to cin and cout, and thus, support cascading operations. For example,

cout 

Chủ Đề