I l@ve RuBoard |
![]() ![]() |
16.2 Conversion RoutinesSo 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.
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
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 " 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.
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.
The more advanced set of manipulators (see Table 16-5) is defined in the header file <iomanip>.
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::showpos|std::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 |
![]() ![]() |