2.3 C++ streams

C++ streams are classes that wrap around the basic stream. However, because of the flexibility of inheritance and specialization, a ``stream'' in C++ can represent a file or anything that can be accessed sequentially, such as a string! This flexibility makes it easy to write stream (superclass) based method or subroutines, and make it work with stream subclasses that specialize in different kinds of FIFO based storage.

One huge advantage of C++ streams over C file streams is typed input and output conversion. In C, the printf and scanf family of subroutines do not perform any parameter checks. This makes it possible to pass an incorrect parameter. In the case of scanf it can easily to memory corruption. In the case of printf, if the format string is contructed from user input, it opens up all kind of vulnerability issues.

None of these problems exist with the C++ streams, as there is no such thing as a format string. All input/output operators are typed.

One slight disadvantage of C++ streams is the difficulty of format specification. With printf and scanf, the specification of decimal numbers versus hexadecimal numbers is fairly ``quick and easy''. However, in C++ streams, it involves a bit more code.

Copyright © 2006-10-16 by Tak Auyeung