Compartilhar via


<ios> functions

 

The latest version of this topic can be found at <ios> functions.

defaultfloat boolalpha dec
fixed hex internal
left noboolalpha noshowbase
noshowpoint noshowpos noskipws
nounitbuf nouppercase oct
right scientific showbase
showpoint showpos skipws
unitbuf uppercase

boolalpha

Specifies that variables of type bool appear as true or false in the stream.

ios_base& boolalpha(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

By default, variables of type bool are displayed as 1 or 0.

boolalpha effectively calls str.setf( ios_base::boolalpha), and then returns str.

noboolalpha reverses the effect of boolalpha.

Example

// ios_boolalpha.cpp  
// compile with: /EHsc  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   bool b = true;  
   cout << b << endl;  
   boolalpha( cout );  
   cout << b << endl;  
   noboolalpha( cout );  
   cout << b << endl;  
   cout << boolalpha << b << endl;  
}  
1  
true  
1  
true  

dec

Specifies that integer variables appear in base 10 notation.

ios_base& dec(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

By default, integer variables are displayed in base 10.

dec effectively calls str``.setf( ios_base::dec, ios_base::basefield), and then returns str.

Example

// ios_dec.cpp  
// compile with: /EHsc  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   int i = 100;  
  
   cout << i << endl;   // Default is base 10  
   cout << hex << i << endl;     
   dec( cout );  
   cout << i << endl;  
   oct( cout );  
   cout << i << endl;  
   cout << dec << i << endl;  
}  
100  
64  
100  
144  
100  

defaultfloat

Configures the flags of an ios_base object to use a default display format for float values.

ios_base& defaultfloat(ios_base& _Iosbase);

Parameters

_Iosbase
An ios_base object.

Remarks

The manipulator effectively calls _I osbase.ios_base::unsetf(ios_base::floatfield), then returns _I osbase.

fixed

Specifies that a floating-point number is displayed in fixed-decimal notation.

ios_base& fixed(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

fixed is the default display notation for floating-point numbers. scientific causes floating-point numbers to be displayed using scientific notation.

The manipulator effectively calls * str.*setf( ios_base::fixed, ios_base::floatfield), and then returns str.

Example

// ios_fixed.cpp  
// compile with: /EHsc  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   float i = 1.1F;  
  
   cout << i << endl;   // fixed is the default  
   cout << scientific << i << endl;  
   cout.precision( 1 );  
   cout << fixed << i << endl;  
}  
1.1  
1.100000e+000  
1.1  

hex

Specifies that integer variables shall appear in base 16 notation.

ios_base& hex(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

By default, integer variables are displayed in base 10 notation. dec and oct also change the way integer variables appear.

The manipulator effectively calls str.setf( ios_base::hex, ios_base::basefield), and then returns str.

Example

See dec for an example of how to use hex.

internal

Causes a number's sign to be left justified and the number to be right justified.

ios_base& internal(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which str is derived.

Remarks

showpos causes the sign to display for positive numbers.

The manipulator effectively calls str. setf( ios_base::internal, ios_base::adjustfield), and then returns str.

Example

// ios_internal.cpp  
// compile with: /EHsc  
#include <iostream>  
#include <iomanip>  
  
int main( void )   
{  
   using namespace std;  
   float i = -123.456F;  
   cout.fill( '.' );  
   cout << setw( 10 ) << i << endl;  
   cout << setw( 10 ) << internal << i << endl;  
}  
..-123.456  
-..123.456  

left

Causes text that is not as wide as the output width to appear in the stream flush with the left margin.

ios_base& left(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

The manipulator effectively calls str.setf( ios_base::left, ios_base::adjustfield), and then returns str.

Example

// ios_left.cpp  
// compile with: /EHsc  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   double f1= 5.00;  
   cout.width( 20 );   
   cout << f1 << endl;  
   cout << left << f1 << endl;  
}  
                   5  
5  

noboolalpha

Specifies that variables of type bool appear as 1 or 0 in the stream.

ios_base& noboolalpha(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

By default, noboolalpha is in effect.

noboolalpha effectively calls str.unsetf( ios_base::boolalpha), and then returns str.

boolalpha reverses the effect of noboolalpha.

Example

See boolalpha for an example of using noboolalpha.

noshowbase

Turns off indicating the notational base in which a number is displayed.

ios_base& noshowbase(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

noshowbase is on by default. Use showbase to indicate the notational base of numbers.

The manipulator effectively calls str.unsetf( ios_base::showbase), and then returns str.

Example

See showbase for an example of how to use noshowbase.

noshowpoint

Displays only the whole-number part of floating-point numbers whose fractional part is zero.

ios_base& noshowpoint(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

noshowpoint is on by default; use showpoint and precision to display zeros after the decimal point.

The manipulator effectively calls str.unsetf( ios_base::showpoint), and then returns str.

Example

// ios_noshowpoint.cpp  
// compile with: /EHsc  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   double f1= 5.000;  
   cout << f1 << endl;   // noshowpoint is default  
   cout.precision( 4 );  
   cout << showpoint << f1 << endl;  
   cout << noshowpoint << f1 << endl;  
}  
5  
5.000  
5  

noshowpos

Causes positive numbers to not be explicitly signed.

ios_base& noshowpos(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

noshowpos is on by default.

The manipulator effectively calls str.unsetf( ios_base::showps), then returns str.

Example

See showpos for an example of using noshowpos.

noskipws

Cause spaces to be read by the input stream.

ios_base& noskipws(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

By default, skipws is in effect. When a space is read in the input stream, it signals the end of the buffer.

The manipulator effectively calls str.unsetf( ios_base::skipws), and then returns str.

Example

// ios_noskipws.cpp  
// compile with: /EHsc  
#include <iostream>  
#include <string>  
  
int main() {  
   using namespace std;     
   string s1, s2, s3;  
   cout << "Enter three strings: ";  
   cin >> noskipws >> s1 >> s2 >> s3;  
   cout << "." << s1  << "." << endl;  
   cout << "." << s2 << "." << endl;  
   cout << "." << s3 << "." << endl;     
}  

nounitbuf

Causes output to be buffered and processed on when the buffer is full.

ios_base& nounitbuf(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

unitbuf causes the buffer to be processed when it is not empty.

The manipulator effectively calls str.unsetf( ios_base::unitbuf), and then returns str.

nouppercase

Specifies that hexadecimal digits and the exponent in scientific notation appear in lowercase.

ios_base& nouppercase(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

The manipulator effectively calls str.unsetf( ios_base::uppercase), and then returns str.

Example

See uppercase for an example of using nouppercase.

oct

Specifies that integer variables appear in base 8 notation.

ios_base& oct(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

By default, integer variables are displayed in base 10 notation. dec and hex also change the way integer variables appear.

The manipulator effectively calls str.setf( ios_base::oct, ios_base::basefield), and then returns str.

Example

See dec for an example of how to use oct.

Causes text that is not as wide as the output width to appear in the stream flush with the right margin.

ios_base& right(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

left also modifies the justification of text.

The manipulator effectively calls str.setf( ios_base::right, ios_base::adjustfield), and then returns str.

Example

// ios_right.cpp  
// compile with: /EHsc  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   double f1= 5.00;  
   cout << f1 << endl;  
   cout.width( 20 );  
   cout << f1 << endl;  
   cout.width( 20 );  
   cout << left << f1 << endl;  
   cout.width( 20 );  
   cout << f1 << endl;  
   cout.width( 20 );  
   cout << right << f1 << endl;  
   cout.width( 20 );  
   cout << f1 << endl;  
}  
5  
                   5  
5                     
5                     
                   5  
                   5  

scientific

Causes floating-point numbers to be displayed using scientific notation.

ios_base& scientific(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

By default, fixed notation is in effect for floating-point numbers.

The manipulator effectively calls str.setf( ios_base::scientific, ios_base::floatfield), and then returns str.

Example

// ios_scientific.cpp  
// compile with: /EHsc  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   float i = 100.23F;  
  
   cout << i << endl;  
   cout << scientific << i << endl;  
}  
100.23  
1.002300e+002  

showbase

Indicates the notational base in which a number is displayed.

ios_base& showbase(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

The notational base of a number can be changed with dec, oct, or hex.

The manipulator effectively calls str.setf( ios_base::showbase), and then returns str.

Example

// ios_showbase.cpp  
// compile with: /EHsc  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   int j = 100;  
  
   cout << showbase << j << endl;   // dec is default  
   cout << hex << j << showbase << endl;  
   cout << oct << j << showbase << endl;  
  
   cout << dec << j << noshowbase << endl;  
   cout << hex << j << noshowbase << endl;  
   cout << oct << j << noshowbase << endl;  
}  
100  
0x64  
0144  
100  
64  
144  

showpoint

Displays the whole-number part of a floating-point number and digits to the right of the decimal point even when the fractional part is zero.

ios_base& showpoint(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

By default, noshowpoint is in effect.

The manipulator effectively calls str.setf( ios_base::showpoint), and then returns str.

Example

See noshowpoint for an example of using showpoint.

showpos

Causes positive numbers to be explicitly signed.

ios_base& showpos(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

noshowpos is the default.

The manipulator effectively calls str.setf( ios_base::showpos), and then returns str.

Example

// ios_showpos.cpp  
// compile with: /EHsc  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   int i = 1;  
  
   cout << noshowpos << i << endl;   // noshowpos is default  
   cout << showpos << i << endl;  
}  
1  
+1  

skipws

Cause spaces to not be read by the input stream.

ios_base& skipws(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which _ Str is derived.

Remarks

By default, skipws is in effect. noskipws will cause spaces to be read from the input stream.

The manipulator effectively calls str.setf( ios_base::skipws), and then returns str.

Example

#include <iostream>  
#include <string>  
  
int main( )   
{  
   using namespace std;  
   char s1, s2, s3;  
   cout << "Enter three characters: ";  
   cin >> skipws >> s1 >> s2 >> s3;  
   cout << "." << s1  << "." << endl;  
   cout << "." << s2 << "." << endl;  
   cout << "." << s3 << "." << endl;  
}  
  
1 2 3  
  
  
      1 2 3.1.  
.2.  
.3.  

unitbuf

Causes output to be processed when the buffer is not empty.

ios_base& unitbuf(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which str is derived.

Remarks

Note that endl also flushes the buffer.

nounitbuf is in effect by default.

The manipulator effectively calls str.setf( ios_base::unitbuf), and then returns str.

uppercase

Specifies that hexadecimal digits and the exponent in scientific notation appear in uppercase.

ios_base& uppercase(ios_base& str);

Parameters

str
A reference to an object of type ios_base, or to a type that inherits from ios_base.

Return Value

A reference to the object from which str is derived.

Remarks

By default, nouppercase is in effect.

The manipulator effectively calls str.setf( ios_base::uppercase), and then returns str.

Example

// ios_uppercase.cpp  
// compile with: /EHsc  
#include <iostream>  
  
int main( void )   
{  
   using namespace std;  
  
   double i = 1.23e100;  
   cout << i << endl;  
   cout << uppercase << i << endl;  
  
   int j = 10;  
   cout << hex << nouppercase << j << endl;  
   cout << hex << uppercase << j << endl;  
}  
1.23e+100  
1.23E+100  
a  
A  

See Also

<ios>