Apache C++ Standard Library User's Guide

## 40.1 Overview

File stream buffers are responsible for the transport of characters to and from an external device. In some cases, the character encoding used internally by your program and externally on the device will differ. Hence the file stream buffer must convert characters from one encoding to another each time it reads from or writes to the external device. (Section 23.3 gives a detailed discussion of character encodings and explains some typical code conversions. If you are not familiar with code conversions, we recommend that you read about them before delving into the details of implementing one in this section.)

A code conversion is not performed by the file stream buffer itself. This task is encapsulated in a code conversion facet. Each time the file stream buffer has to convert characters, it consults its locale's code conversion facet for the actual conversion. For this reason, file stream buffers and code conversion facets must work together closely, and the file stream buffer depends on its locale's code conversion facet.

This clear separation of responsibilities enables you to change a file stream's behavior substantially, without touching the file stream class itself. All you must do is provide a special code conversion facet. In doing so, you turn an ordinary file stream into one that converts, say, EBCDIC files on a mainframe's file system into a stream of ASCII characters for internal processing.

However, the task of implementing a code conversion facet requires a thorough understanding of the way file stream buffers and code conversion facets interact. In this section, we use two examples to explain the principles of this interaction.

Before we move on to the examples, let's get an overview of the different kinds of code conversions. As we show later on, different types of code conversions require different kinds of implementations.