Continuing on binary input and output in c++
Floating point number can be display in 3 formatting modes: The precision is interpreted differently in default and non-default modes due to legacy. You can also use ostream 's member function precision n e.
File IO requires an additional step to connect the file to the stream i. By default, opening an output file creates a new file if the filename does not exist; or truncates it clear its content and starts writing as an empty file. The available file mode flags are:. You can set multiple flags via bit-or operator, e. For output, the default is ios:: For input, the default is ios:: We need to use read and write member functions for binary file file mode of ios:: Random access file is associated with a file pointer, which can be moved directly to any location in the file.
Random access is crucial in certain applications such as databases and indexes. You can position the input pointer via seekg and output pointer via seekp. Each of them has two versions: The string streams is based on ostringstream subclass of ostream , istringstream subclass of istream and bi-directional stringstream subclass of iostream.
IO operations are defined for each of the type. If IO operations are not defined for a particular type, compiler will generate an error. The same set of operations can be applied to different types of IO devices. Construct a stream object. Connect Associate the stream object to an actual IO device e.
Some functions convert the data between the external format and internal format formatted IO ; while other does not unformatted or binary IO. Disconnect Dissociate the stream to the actual IO device e. Free the stream object. Take note that istream and ostream are unidirectional streams; whereas iostream is bidirectional.
The unformatted output functions e. It uses the terminating null character to decide the end of the char array. In unformatting input, such as get , getlin , read , it reads the characters as they are, without conversion. It uses whitespace as delimiter and adds a terminating null character to the C-string. Flushing the Input Buffer - ignore You can use the ignore to discard characters in the input buffer: The last input operation failed to read the expected characters or output operation failed to write the expected characters, e.
Absence of above error with value of 0. However, it is more convenience to use these public member functions of ios class: By default, the values are displayed with a field-width just enough to hold the text, without additional leading or trailing spaces. You need to provide spaces between the values, if desired. For integers, all digits will be displayed, by default. This default precision of 6 digits include all digits before and after the decimal point, but exclude the leading zeros.
Scientific notation E-notation will be used if the exponent is 6 or more or -5 or less. In default mode neither fixed nor scientific used , a floating-point number is displayed in fixed-point notation e. The precision in default mode includes digits before and after the decimal point but exclude the leading zeros.
Fewer digits might be shown as the trailing zeros are not displayed. A format state governs how output values will be printed and how input values will be interpreted, and consists of. Each format flag is designated by name with an ios constant, as shown in the table which follows. The table also gives the purpose of each flag. Note as well that there are certain groups of flags, called bit fields , three of which also have names provided by ios constants.
Further details on these bit fields are given in the notes following the table. Some of these flags apply only to input streams, others only to output streams, while some apply to both. For more specific information on this, see the table of manipulators later on this page. As we have noted above, the fieldwidth parameter is not persistent , in that it only applies to the next input or output item.
That begs the question: What about the fill character and the precision value—are they persistent? You should write a short sample program to perform some tests suitable for answering this question.
Both format-state flags and format-state parameters can be controlled either by member functions of the stream classes, or by stream manipulators. This gives the programmer many options, and with this come the eternal compromises that have to be made between extra flexibility with extra power, and extra complexity with potential confusion.
The following three stream member functions note that the first two have two versions each may be used to deal with format-state flags. The following three stream member functions each with two versions may be used to deal with format-state parameters. A manipulator is a function object that can be used as an operand to an input or output operator to manipulate a stream.
There are two kinds of manipulators, those that take parameters and those that do not. Many manipulators have names that suggest the format-state flags that they set, or unset, or the format-state parameters they give values to, for that is what many but not all manipulators do. The following tables show the available manipulators, and indicate, where applicable, the relationships between format-state flags and manipulators, or between format-state parameters and manipulators.
The last column in each table indicates whether a given manipulator and any associated flag or parameter applies to input streams only, to output streams only, or to both input and output streams.
Because we have two distinct ways of setting both format-state flags and format-state parameters, the obvious question of when to use which method arises.
The key thing to keep in mind when answering this question is this: The member functions all return the current state of whatever you happen to be setting, if you are using the version of one of these functions that takes one or more parameters. The manipulators, on the other hand, do not return the current state. So, if you wish to retain the current state for any reason to reset the stream to that original state later, for example , then use one or more member functions to set the state; otherwise, it doesn't matter.
See this part of the Standard Library Reference on this site for additional details on manipulators without parameters. See this part of the Standard Library Reference on this site for additional details on manipulators with parameters. Files fall into two major categories—textfiles and binary files—whose differences are summarized in the following table.
An expanded form of the UML class diagram given previously is show below, and illustrates the additional classes that are needed to deal with files. Before a program can gain access to a physical file for input or output, a "stream object" must be created and connected to the designated physical file.
This can be done in essentially two ways: Also, files may be "opened" in various "modes", which are summarized in the following table. You may also occasionally encounter the non-standard modes ios:: When a program attempts to read input data from a stream, many things can go wrong.
For example, the physical source that the program is looking for may not even exist, or may be inaccessible for some reason.