Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
In the first line of the above code, the double variable The syntax given in Form 1 takes one argument. By this
amount is declared and initialized with a number having
statement flag is set for the outputs through
five characters (including the decimal point). On Line 2,
ostreamObject and the flag will persist until it is
By: Asaye Chemeda Email: asayechemeda@yahoo.com 59
unset. As part of the argument the function name where Program 4.5[Correct].cpp
the flag belongs (ios) and the scope resolution operator
(::) should always precede the flag.
The syntax given in Form 2 takes two arguments. By
this statement previous settings corresponding to flag2
will be cleared first and flag1 will be set as a flag for the
coming output operations into ostreamObject and
the flag given by flag1 will persist until it is unset. Both
arguments should be preceded by the function name
where the flag belongs (ios) and the scope resolution
operator (::).
A number of flags can also be combined as a single setf
function argument using the OR operator. This avoids
writing multiple lines which could be written to change
the setting for each flag. The syntax for such setf
function with combined flags when invoked by the cout The output for the above program is shown below. Since
stream is given below. the only formatting functions used in the above program
are width and fill and since the effect of these
functions was discussed in detail, no further discussion
The frequently used flags in ios class are: will be made on the output.
left makes the output left-justified.
right makes the output right-justified.
showpoint enforces displaying of trailing zeroes.
showpos displays the + sign when positive numbers
are displayed. Now, let us introduce the left, showpoint and
fixed puts the output in fixed number notation. showpos flags in the program and analyze the output. A
scientific puts a floating-point output in scientific Boolean variable isPrintComplete was also
notation.
declared and initialized with a value of false. This
boolalpha allows input and output of Boolean
variable attains a truth value of true after all the desired
values by using the keywords true and false.
dec displays an output as decimal (base 10) number. output are printed.
oct displays an integer output as octal (base 12) Program 4.6[Correct].cpp
number.
hex displays an integer output as hexadecimal (base 16)
number.
Creation of a stream
Three types of streams can be created for file I/O. An
input stream for input operations, an output stream for
output operations and an input/output stream for both
input and output stream operations. Each type of the The above approach simultaneously creates streams and
streams is created by declaring a variable with the data associates them with the file which has fileName.
type of the class corresponding to the stream. The class
corresponding to the input stream is ifstream. If the file is not in the same directory as the directory
which the program points to, the full path should be
Whereas, the class corresponding to the output stream is
mentioned with fileName. If a file with a specified file
ofstream. The fstream class corresponds to
name is not available in the directory, the input stream
input/output stream. Once a variable is declared with a
leads to a program in fail state while the output stream
data type of these classes, it will be used as a stream like
creates a new. Therefore, including a code which checks
the standard streams such as cin and cout. The name
the availability of the file with the specified file name in
of the variable can be any qualified identifier.
the program is important to avoid a possible input stream
The syntax to create an input, output and input/output failure. When an output stream interacts with an existing
streams is: file as shown above, it first erases all the data in the file.
ios::beg location specifier for beginning of The file will now look like:
file
ios::cur location specifier for current
location
ios::end location specifier for end of file
The program which uses iFile.txt as an input file
and oFile.txt as an output file is:
Even if the seekg and seekp functions are useful in
pointing to a specific file location within a file, Program 4.13[Correct].cpp
determining the value of an offset is usually too sensitive
for a programmer to handle when the file is in text mode.
This is because a number of character translations into
bytes will take place by the compiler when a file is opened
in text mode. As a result, in files opened in text mode,
every character including white spaces, tabs and new lines
will have their own translations. And it is difficult for a
programmer to keep track of these and predict where
exactly the program might point. As a result, these
functions are not often used with files opened in text
mode. However, for files opened in binary mode, the
functions can easily be used.
Sometimes it may be desired to get the current location
where I/O operation is taking place. In those cases, the
tellg and tellp functions become useful. The
tellg function returns the current location of input
operation on the input file and tellp is its counterpart
for an output operation on the output file. The syntax
used for these functions are: