16.2 Conversion Routines

I l @ ve RuBoard

So far we have just considered writing characters and strings. In this section, we consider some of the more sophisticated I/O operations: conversions.

To write a number to a printer or terminal, you must convert the number to characters. The printer understands only characters, not numbers . For example, the number 567 must be converted to the three characters "5", "6", and "7" to be printed.

The << operator is used to convert data to characters and put them in a file. This function is extremely flexible. It can convert a simple integer into a fixed- or variable- size string as a hex, octal, or decimal number with left or right justification. So far you've been using the default conversion for your output. It serves pretty well, but if you want to control your output exactly, you need to learn about conversion flags.

The member functions setf and unsetf are used to set and clear the flags that control the conversion process. The general form of the functions is:

   file_var   .setf(flags);   // Set flags   file_var   .unsetf(flags); // Clear flags 

Table 16-3 lists the various flags and their meanings.

Table 16-3. I/O conversion flags

Flag

Meaning

std::ios::skipws

Skip leading whitespace characters on input.

std::ios::left

Output is left justified.

std::ios::right

Output is right justified.

std::ios::internal

Numeric output is padded by inserting a fill character between the sign or base character and the number itself.

std::ios::boolalpha

Use the character version of true and false ("true", "false") for input and output.

std::ios::dec

Output numbers in base 10, decimal format.

std::ios::oct

Output numbers in base 8, octal format.

std::ios::hex

Output numbers in base 16, hexadecimal format.

std::ios::showbase

Print out a base indicator at the beginning of each number. For example, hexadecimal numbers are preceded with "0x".

std::ios::showpoint

Show a decimal point for all floating-point numbers whether or not it's needed.

std::ios::uppercase

When converting hexadecimal numbers, show the digits A-F as uppercase.

std::ios::showpos

Put a plus sign before all positive numbers.

std::ios::scientific

Convert all floating-point numbers to scientific notation on output.

std::ios::fixed

Convert all floating-point numbers to fixed point on output.

std::ios::unitbuf

Buffer output. (More on this later.)

If you want to output a number in hexadecimal format, all you have to do is this:

 number = 0x3FF; std::cout << "Dec: " << number << '\n'; std::cout.setf(std::ios::hex); std::cout << "Hex: " << number << '\n'; std::cout.setf(std::ios::dec); 

When run, this program produces the output:

 Dec: 1023 Hex: 3ff 

People normally expect the output mode to be decimal, so it is a good idea to reset the mode after each output to avoid later confusion.

When converting numbers to characters, the member function:

 int   file_var   .width(int size); 

determines the minimum characters to use. For example, the number 3 would normally convert to the character string "3" (note the lack of spaces). If the width is set to four, the result would be " " where represents a single space.

The member function:

 int   file_var   .precision(int digits); 

controls how many digits are printed after the decimal point.

Finally, the function:

 char   file_var   .fill(char pad); 

determines the fill character. This character is used for padding when a number is smaller than the specified width.

Some of these flags and parameters are reset after each output call and some are not. Which flags are permanent and which are temporary seems to change from compiler to compiler. In general, don't assume anything is going to remain set and you'll be okay. (Just because you're paranoid doesn't mean the compiler isn't out to get you.)

These functions can be called directly, or you can use an I/O manipulator . An I/O manipulator is a special function that can be used in an I/O statement to change the formatting. You can think of a manipulator as a magic bullet that, when sent through an input or output file, changes the state of the file. A manipulator doesn't cause any output; it just changes the state. For example, the manipulator hex changes the output conversion to hexadecimal.

 #include <iostream> number = 0x3FF; std::cout << "Number is " << std::hex << number << std::dec << '\n'; 

The header file <iostream> defines a basic set of manipulators. Table 16-4 contains a list of these manipulators.

Table 16-4. I/O manipulators

Manipulator

Description

std::dec

Output numbers in decimal format.

std::hex

Output numbers in hexadecimal format.

std::oct

Output numbers in octal format.

std::ws

Skip whitespace on input.

std::endl

Output end-of-line

std::ends

Output end-of-string (`\0').

std::flush

Force any buffered output out. (See Chapter 17 for an explanation of how to use this function).

The more advanced set of manipulators (see Table 16-5) is defined in the header file <iomanip> .

Table 16-5. I/O manipulators

Manipulator

Description

std::setiosflags(long flags)

Set selected conversion flags.

std::resetiosflags(long flags)

Reset selected flags.

std::setbase(int base)

Set conversion base to 8, 10, or 16. Sort of a generalized std:: dec , std:: hex , std:: oct .

std::setw(int width)

Set the width of the output.

std::setprecision(int precision)

Set the precision of floating-point output.

std::setfill(char ch)

Set the fill character.

Example 16-3 shows how some of the I/O manipulators may be used.

Example 16-3. io/io.cpp
 #include <iostream> #include <iomanip> int main(  ) {     int number = 12;    // A number to output     float real = 12.34; // A real number     std::cout << "123456789012345678901234567890\n"; // output ruler     std::cout << number << "<-\n";     std::cout << std::setw(5) << number << "<-\n";     std::cout << std::setw(5) << std::setfill('*') <<          number << "<-\n";     std::cout << std::setiosflags(std::ios::showposstd::ios::left) <<          std::setw(5) << number << "<-\n";     std::cout << real << "<-\n";     std::cout << std::setprecision(1) <<          std::setiosflags(std::ios::fixed) << real << "<-\n";     std::cout << std::setiosflags(std::ios::scientific) << real << "<-\n";     return (0); } 

The output of this program is:

 123456789012345678901234567890 12<-    12<- ***12<- +12**<- 12.34<- 12.3<- 1e+01<- 
I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net