Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library User's Guide

34.4 Copies of the Stream Buffer

The previous section showed how you can read the content of a file in its entirety by using the rdbuf() member function. Let us now explore a variation of that example. Imagine another file containing some sort of header information that needs to be analyzed before we start appending the file. Instead of writing the current file content to the standard output stream, we want to process the content before we start adding to it. The easiest way to put the entire file content into a memory location for further processing is by creating a string stream and inserting the file's stream buffer into the string stream:

//1The easiest way to put the entire file content into a memory location for further processing is by creating a string stream, and
//2Inserting the file's stream buffer into the string stream.
//3We now have the usual facilities of an input stream for reading and analyzing the header information; that is, operator>>(), read(), get(), and so on.

In cases where this procedure is insufficient, you should create a string that contains the header information and process the header by means of the string operations find(), compare(), etc.

If the header contains binary data instead of text, even a string will probably not suffice. Here you would want to see the header as a plain byte sequence, that is, an ordinary char* buffer. But note that a code conversion might already have been performed, depending on the locale attached to the file stream. In cases where you want to process binary data, you must ensure that the attached locale has a non-converting code conversion facet:

A note on efficiency: if the header information is extensive, you must consider the number of copy operations performed in the previous example. Figure 34 shows how these copies are made:

Figure 34: Copies of the file content

The content of the file is copied into the string stream's buffer when the pointer obtained through rdbuf() is inserted to the string stream. A second copy is created when the string stream's function str() is called. The call to the string's function data() does not create yet another copy, but returns a pointer to the string's internal data.



Previous fileTop of DocumentContentsIndex pageNext file