Whole document tree Chapter 27: Input/OutputChapter 27 deals with iostreams and all their subcomponents and extensions. All kinds of fun stuff. Contents
Copying a fileSo you want to copy a file quickly and easily, and most important, completely portably. And since this is C++, you have an open ifstream (call it IN) and an open ofstream (call it OUT): #include <fstream> std::ifstream IN ("input_file"); std::ofstream OUT ("output_file"); Here's the easiest way to get it completely wrong: OUT << IN;For those of you who don't already know why this doesn't work (probably from having done it before), I invite you to quickly create a simple text file called "input_file" containing the sentence The quick brown fox jumped over the lazy dog.surrounded by blank lines. Code it up and try it. The contents of "output_file" may surprise you. Seriously, go do it. Get surprised, then come back. It's worth it. The thing to remember is that the Why a pointer to streambuf and not just a streambuf? Well,
the [io]streams hold pointers (or references, depending on the
implementation) to their buffers, not the actual
buffers. This allows polymorphic behavior on the part of the buffers
as well as the streams themselves. The pointer is easily retrieved
using the OUT << IN.rdbuf(); So what was happening with OUT<<IN? Undefined behavior, since that particular << isn't defined by the Standard. I have seen instances where it is implemented, but the character extraction process removes all the whitespace, leaving you with no blank lines and only "Thequickbrownfox...". With libraries that do not define that operator, IN (or one of IN's member pointers) sometimes gets converted to a void*, and the output file then contains a perfect text representation of a hexidecimal address (quite a big surprise). Others don't compile at all. Also note that none of this is specific to o*f*streams. The operators shown above are all defined in the parent basic_ostream class and are therefore available with all possible descendents. Return to top of page or to the FAQ. The buffering is screwing up my program!First, are you sure that you understand buffering? Particularly the fact that C++ may not, in fact, have anything to do with it? The rules for buffering can be a little odd, but they aren't any different from those of C. (Maybe that's why they can be a bit odd.) Many people think that writing a newline to an output stream automatically flushes the output buffer. This is true only when the output stream is, in fact, a terminal and not a file or some other device -- and that may not even be true since C++ says nothing about files nor terminals. All of that is system-dependent. (The "newline-buffer-flushing only occurring on terminals" thing is mostly true on Unix systems, though.) Some people also believe that sending output << "a line of text" << endl; output << some_data_variable << endl; output << "another line of text" << endl;The proper thing to do in this case to just write the data out and let the libraries and the system worry about the buffering. If you need a newline, just write a newline: output << "a line of text\n" << some_data_variable << '\n' << "another line of text\n";I have also joined the output statements into a single statement. You could make the code prettier by moving the single newline to the start of the quoted text on the thing line, for example. If you do need to flush the buffer above, you can send an
output << ...... << flush; // can use std::flush manipulator output.flush(); // or call a member fn On the other hand, there are times when writing to a file should be like writing to standard error; no buffering should be done because the data needs to appear quickly (a prime example is a log file for security-related information). The way to do this is just to turn off the buffering before any I/O operations at all have been done, i.e., as soon as possible after opening: std::ofstream os ("/foo/bar/baz"); std::ifstream is ("/qux/quux/quuux"); int i; os.rdbuf()->pubsetbuf(0,0); is.rdbuf()->pubsetbuf(0,0); ... os << "this data is written immediately\n"; is >> i; // and this will probably cause a disk read Since all aspects of buffering are handled by a streambuf-derived
member, it is necessary to get at that member with A great deal of this is implementation-dependent. For example,
A last reminder: there are usually more buffers involved than just those at the language/library level. Kernel buffers, disk buffers, and the like will also have an effect. Inspecting and changing those are system-dependent. Return to top of page or to the FAQ. Binary I/OThe first and most important thing to remember about binary I/O is
that opening a file with Sorry. Them's the breaks. This isn't going to try and be a complete tutorial on reading and writing binary files (because "binary" covers a lot of ground), but we will try and clear up a couple of misconceptions and common errors. First, Second, using Third, using the Notice how all the problems here are due to the inappropriate use of formatting functions and classes to perform something which requires that formatting not be done? There are a seemingly infinite number of solutions, and a few are listed here:
How to go about using streambufs is a bit beyond the scope of this document (at least for now), but while streambufs go a long way, they still leave a couple of things up to you, the programmer. As an example, byte ordering is completely between you and the operating system, and you have to handle it yourself. Deriving a streambuf or filebuf
class from the standard ones, one that is specific to your data
types (or an abstraction thereof) is probably a good idea, and
lots of examples exist in journals and on Usenet. Using the
standard filebufs directly (either by declaring your own or by
using the pointer returned from an fstream's One area that causes problems is trying to do bit-by-bit operations
with filebufs. C++ is no different from C in this respect: I/O
must be done at the byte level. If you're trying to read or write
a few bits at a time, you're going about it the wrong way. You
must read/write an integral number of bytes and then process the
bytes. (For example, the streambuf functions take and return
variables of type Another area of problems is opening text files in binary mode. Generally, binary mode is intended for binary files, and opening text files in binary mode means that you now have to deal with all of those end-of-line and end-of-file problems that we mentioned before. An instructive thread from comp.lang.c++.moderated delved off into this topic starting more or less at this article and continuing to the end of the thread. (You'll have to sort through some flames every couple of paragraphs, but the points made are good ones.) What is this <sstream>/stringstreams thing?Stringstreams (defined in the header The quick definition is: they are siblings of ifstream and ofstream,
and they do for This only works if you've written your
If you are a user of the strstream classes, you need to update
your code. You don't have to explicitly append Deriving a stream bufferCreating your own stream buffers for I/O can be remarkably easy. If you are interested in doing so, we highly recommend two very excellent books: Standard C++ IOStreams and Locales by Langer and Kreft, ISBN 0-201-18395-1, and The C++ Standard Library by Nicolai Josuttis, ISBN 0-201-37926-0. Both are published by Addison-Wesley, who isn't paying us a cent for saying that, honest. Here is a simple example, io/outbuf1, from the Josuttis text. It transforms everything sent through it to uppercase. This version assumes many things about the nature of the character type being used (for more information, read the books or the newsgroups): #include <iostream> #include <streambuf> #include <locale> #include <cstdio> class outbuf : public std::streambuf { protected: /* central output function * - print characters in uppercase mode */ virtual int_type overflow (int_type c) { if (c != EOF) { // convert lowercase to uppercase c = std::toupper(static_cast<char>(c),getloc()); // and write the character to the standard output if (putchar(c) == EOF) { return EOF; } } return c; } }; int main() { // create special output buffer outbuf ob; // initialize output stream with that output buffer std::ostream out(&ob); out << "31 hexadecimal: " << std::hex << 31 << std::endl; return 0; }Try it yourself! More on binary I/OTowards the beginning of February 2001, the subject of "binary" I/O was brought up in a couple of places at the same time. One notable place was Usenet, where James Kanze and Dietmar Kühl separately posted articles on why attempting generic binary I/O was not a good idea. (Here are copies of Kanze's article and Kühl's article.) Briefly, the problems of byte ordering and type sizes mean that
the unformatted functions like The entire Usenet thread is instructive, and took place under the subject heading "binary iostreams" on both comp.std.c++ and comp.lang.c++.moderated in parallel. Also in that thread, Dietmar Kühl mentioned that he had written a pair of stream classes that would read and write XDR, which is a good step towards a portable binary format. Pathetic performance? Ditch C.It sounds like a flame on C, but it isn't. Really. Calm down. I'm just saying it to get your attention. Because the C++ library includes the C library, both C-style and C++-style I/O have to work at the same time. For example: #include <iostream> #include <cstdio> std::cout << "Hel"; std::printf ("lo, worl"); std::cout << "d!\n";This must do what you think it does. Alert members of the audience will immediately notice that buffering is going to make a hash of the output unless special steps are taken. The special steps taken by libstdc++, at least for version 3.0,
involve doing very little buffering for the standard streams, leaving
most of the buffering to the underlying C library. (This kind of
thing is tricky to get right.)
The upside is that correctness is ensured. The downside is that
writing through However, the C and C++ standard streams only need to be kept in sync when both libraries' facilities are in use. If your program only uses C++ I/O, then there's no need to sync with the C streams. The right thing to do in this case is to call #include any of the I/O headers such as ios, iostream, etc std::ios::sync_with_stdio(false); You must do this before performing any I/O via the C++ stream objects.
Once you call this, the C++ streams will operate independently of the
(unused) C streams. For GCC 3.0, this means that Note, by the way, that the synchronization requirement only applies to
the standard streams ( Threads and I/OI'll assume that you have already read the general notes on library threads, and the notes on threaded container access (you might not think of an I/O stream as a container, but the points made there also hold here). If you have not read them, please do so first. This gets a bit tricky. Please read carefully, and bear with me. StructureAs described here, a wrapper
type called A generic locking mechanism is somewhat in place at the filebuf layer, but is not used in the current code. Providing locking at any higher level is akin to providing locking within containers, and is not done for the same reasons (see the links above). The defaults for 3.0.xThe __basic_file type is simply a collection of small wrappers around
the C stdio layer (again, see the link under Structure). We do no
locking ourselves, but simply pass through to calls to So, for 3.0, the question of "is multithreading safe for I/O" must be answered with, "is your platform's C library threadsafe for I/O?" Some are by default, some are not; many offer multiple implementations of the C library with varying tradeoffs of threadsafety and efficiency. You, the programmer, are always required to take care with multiple threads. (As an example, the POSIX standard requires that C stdio FILE*
operations are atomic. POSIX-conforming C libraries (e.g, on Solaris
and GNU/Linux) have an internal mutex to serialize operations on
FILE*s. However, you still need to not do stupid things like calling
So, if your platform's C library is threadsafe, then your
The futureAs already mentioned here, a second choice is available for I/O implementations: libio. This is disabled by default, and in fact will not currently work due to other issues. It will be revisited, however. The libio code is a subset of the guts of the GNU libc (glibc) I/O
implementation. When libio is in use, the Also, the libio internal locks are used. This requires pulling in large chunks of glibc, such as a pthreads implementation, and is one of the issues preventing widespread use of libio as the libstdc++ cstdio implementation. But we plan to make this work, at least as an option if not a future default. Platforms running a copy of glibc with a recent-enough version will see calls from libstdc++ directly into the glibc already installed. For other platforms, a copy of the libio subsection will be built and included in libstdc++. AlternativesDon't forget that other cstdio implemenations are possible. You could easily write one to perform your own forms of locking, to solve your "interesting" problems. See license.html for copying conditions. Comments and suggestions are welcome, and may be sent to the libstdc++ mailing list. |