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. Furthermore, the header provided manipulators such as setw(), setprecision() setfill() and setbase() for formatting. Disconnect (Dissociate) the stream to the actual IO device (e.g., close the file).Ĭ++ IO Headers, Templates and Classes HeadersĬ++ IO is provided in headers (which included, , and ), (for file IO), and (for string IO).Some functions convert the data between the external format and internal format (formatted IO) while other does not (unformatted or binary IO). Perform input/output operations on the stream, via the functions defined in the stream's pubic interface in a device independent manner.Connect (Associate) the stream object to an actual IO device (e.g., keyboard, console, file, network, another program).To perform input and output, a C++ program: Formatted IO operations are supported via overloading the stream insertion ( >) operators, which presents a consistent public IO interface. In unformatted or low-level IO, bytes are treated as raw bytes and unconverted. In formatted or high-level IO, bytes are grouped and converted to types such as int, double, string or user-defined types. 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.Ĭ++ provides both the formatted and unformatted IO functions. In output operations, data bytes flow from the program to an output sink (such as console, file, network or another program). In input operations, data bytes flow from an input source (such as keyboard, file, network or another program) into the program. The same set of operations can be applied to different types of IO devices.Ĭ/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). C++ IO operations are based on streams of bytes and are device independent.If IO operations are not defined for a particular type, compiler will generate an error. IO operations are defined for each of the type. C++ continues this approach and formalizes IO in libraries such as iostream and fstream. The ANSI C standard formalized these IO functions into Standard IO package ( stdio.h). Instead, it left the IO to the compiler as external library functions (such as printf and scanf in stdio library). In other words, there is no keyword like read or write. The Boost.IoStreams solution might be useful in a context where one already works with Boost.IoStreams though.The C language did not build the input/output facilities into the language. I prefer the standard C++11 solution because it has less dependencies. There are basically just one or two additional code lines to wrap a wide stream around the narrow stream (not counting the includes and the using-statements)! Standard streams are by definition non-copyable, so we must work around that by using boost::ref which is a copyable reference-wrapper (see Design Rationale of Boost.IoStreams).īoth the standard C++11 and the Boost.IoStreams solution are pretty straightforward to use. The boost::ref is required because Boost.IoStreams requires devices to be copyable (a standard stream is just a model of device in terms of Boost.IoStreams). Line 18 creates an instance of that stream which just wraps the std::istream parameter.This type is just used in the following line to create the actual standard-compatible stream type. Line 9 creates a “device” which is a concept that exists solely in the boost::iostreams library.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |