basic_ios

此类模板描述了依赖于模板参数的输入流(属于类模板 basic_istream)和输出流(属于类模板 basic_ostream)通用的存储和成员函数。 (ios_base 类描述不依赖于模板参数的通用内容。)basic_ios<class Elem, class Traits> 类的对象有助于控制具有 Elem 类型的元素的流,该类型的字符特征由 Traits 类决定。

语法

template <class Elem, class Traits>
class basic_ios : public ios_base

参数

Elem
一个字符类型。

Traits
提供有关字符类型的信息的类型默认为 char_traits < Elem >

注解

basic_ios<class Elem, class Traits> 类的对象将存储:

  • 指向 basic_istream<Elem, Traits> 类型的对象的绑定指针。

  • 指向 basic_streambuf<Elem, Traits > 类型的对象的流缓冲区指针。

  • 格式设置信息

  • ios_base 类型的基对象中的流状态信息

  • char_type 类型的对象中的填充字符。

有关详细信息,请参阅 basic_istreambasic_streambuf

构造函数

构造函数 说明
basic_ios 构造 basic_ios 类。

Typedef

类型名称 说明
char_type 模板参数 Elem 的同义词。
int_type Traits::int_type 的同义词。
off_type Traits::off_type 的同义词。
pos_type Traits::pos_type 的同义词。
traits_type 模板参数 Traits 的同义词。

成员函数

成员函数 说明
bad 指示流缓冲区的完整性损失。
clear 清除所有错误标志。
copyfmt 将标志从一个流复制到另一个流。
eof 指示是否已到达流的结尾。
exceptions 指示流将引发哪些异常。
fail 指示从流中提取有效字段失败。
fill 指定或返回在文本宽度小于流宽度时将使用的字符。
good 指示流处于良好状态。
imbue 更改区域设置。
init 通过 basic_ios 构造函数调用。
move 将所有值(指向流缓冲区的指针除外)从参数移动到当前对象。
narrow 查找给定 char_type 的等效字符型。
rdbuf 将流路由到指定缓冲区。
rdstate 读取标志的位状态。
set_rdbuf 分配流缓冲区作为此流对象的读取缓冲区。
setstate 设置其他标志。
swap 将此 basic_ios 对象中的值与另一个 basic_ios 对象中的值进行交换。 不会交换指向流缓冲区的指针。
tie 确保依次处理流。
widen 查找给定字符型的等效 char_type

运算符

运算符 说明
explicit operator bool 允许将 basic_ios 对象作为作 bool。 禁用自动类型转换以防止产生常见的意外副作用。
operator void * 指示流是否仍处于良好状态。
operator! 指示流是否完好无损。

要求

标头<ios>

命名空间:std

basic_ios::bad

指示流缓冲区的完整性损失

bool bad() const;

返回值

如果 rdstate & badbit 为非零,则为 true,否则为 false

有关 badbit 的详细信息,请参阅 ios_base::iostate

示例

// basic_ios_bad.cpp
// compile with: /EHsc
#include <iostream>

int main( void )
{
    using namespace std;
    bool b = cout.bad( );
    cout << boolalpha;
    cout << b << endl;

    b = cout.good( );
    cout << b << endl;
}

basic_ios::basic_ios

构造 basic_ios 类。

explicit basic_ios(basic_streambuf<Elem,  Traits>* sb);
basic_ios();

参数

sb
用于存储输入或输出元素的标准缓冲区。

注解

第一个构造函数通过调用 init(_ Sb) 可初始化其成员对象。 第二个(受保护的)构造函数取消初始化其成员对象。 稍后对 init 的调用必须先初始化此对象,然后才可被安全销毁。

basic_ios::char_type

模板参数 Elem 的同义词。

typedef Elem char_type;

basic_ios::clear

清除所有错误标志。

void clear(iostate state = goodbit, bool reraise = false);
void clear(io_state state);

参数

state
(可选)清除所有标志后你想要设置的标志。 默认为 goodbit

reraise
(可选)指定是否应重新引发这个异常。 默认为 false(将不重新引发异常)。

备注

标志是 goodbitfailbiteofbitbadbit。 使用 goodbadeoffail 测试这些标志

成员函数将存储的流状态信息替换为:

state | (rdbuf != 0 goodbit : badbit)

如果 state&exceptions 为非零,则它会抛出类 failure 的一个对象。

有关详细信息,请参阅 rdbufexceptions

示例

有关使用 clear 的示例,请参阅 rdstategetline

basic_ios::copyfmt

将标志从一个流复制到另一个流。

basic_ios<Elem, Traits>& copyfmt(
const basic_ios<Elem, Traits>& right);

参数

right
要复制其标志的流。

返回值

正在向其复制标志的流的 this 对象。

备注

此成员函数将报告回调事件 erase_event。 然后,它将填充字符、绑定指针和格式设置信息从 right 复制到 *this。 在更改异常掩码之前,它会报告回调事件 copyfmt_event。 复制完成后,如果 state&exceptions 为非零,则该函数将有效地调用带参数 rdstateclear。 它将返回 *this

示例

// basic_ios_copyfmt.cpp
// compile with: /EHsc
#include <iostream>
#include <fstream>

int main( )
{
    using namespace std;
    ofstream x( "test.txt" );
    int i = 10;

    x << showpos;
    cout << i << endl;
    cout.copyfmt( x );
    cout << i << endl;
}

basic_ios::eof

指示是否已到达流的结尾。

bool eof() const;

返回值

如果已到达流的结尾,则为 true,否则为 false

备注

如果 rdstate& eofbit 为非零,则成员函数将返回 true。 有关 eofbit 的详细信息,请参阅 ios_base::iostate

示例

// basic_ios_eof.cpp
// compile with: /EHsc
#include <iostream>
#include <fstream>

using namespace std;

int main( int argc, char* argv[] )
{
    fstream   fs;
    int n = 1;
    fs.open( "basic_ios_eof.txt" );   // an empty file
    cout << boolalpha
    cout << fs.eof() << endl;
    fs >> n;   // Read the char in the file
    cout << fs.eof() << endl;
}

basic_ios::exceptions

指示流将引发哪些异常。

iostate exceptions() const;
void exceptions(iostate Newexcept);
void exceptions(io_state Newexcept);

参数

Newexcept
希望抛出异常的标志。

返回值

当前指定的用于为流抛出异常的标志。

备注

第一个成员函数返回存储的异常掩码。 第二个成员函数在异常掩码中存储 _Except 并返回其先前的存储值。 存储新异常掩码会与调用 clear(rdstate) 一样抛出异常。

示例

// basic_ios_exceptions.cpp
// compile with: /EHsc /GR
#include <iostream>

int main( )
{
    using namespace std;

    cout << cout.exceptions( ) << endl;
    cout.exceptions( ios::eofbit );
    cout << cout.exceptions( ) << endl;
    try
    {
        cout.clear( ios::eofbit );   // Force eofbit on
    }
    catch ( exception &e )
    {
        cout.clear( );
        cout << "Caught the exception." << endl;
        cout << "Exception class: " << typeid(e).name()  << endl;
        cout << "Exception description: " << e.what() << endl;
    }
}
0
1
Caught the exception.
Exception class: class std::ios_base::failure
Exception description: ios_base::eofbit set

basic_ios::fail

指示从流中提取有效字段失败。

bool fail() const;

返回值

如果 rdstate & (badbit|failbit) 为非零,则为 true,否则为 false

有关 failbit 的详细信息,请参阅 ios_base::iostate

示例

// basic_ios_fail.cpp
// compile with: /EHsc
#include <iostream>

int main( void )
{
    using namespace std;
    bool b = cout.fail( );
    cout << boolalpha;
    cout << b << endl;
}

basic_ios::fill

指定或返回在文本宽度小于流宽度时将使用的字符。

char_type fill() const;
char_type fill(char_type Char);

参数

Char
希望用作填充字符的字符。

返回值

当前的填充字符。

备注

第一个成员函数返回存储的填充字符。 第二个成员函数将 Char 存储在填充字符中,并返回其先前的存储值。

示例

// basic_ios_fill.cpp
// compile with: /EHsc
#include <iostream>
#include <iomanip>

int main( )
{
    using namespace std;

    cout << setw( 5 ) << 'a' << endl;
    cout.fill( 'x' );
    cout << setw( 5 ) << 'a' << endl;
    cout << cout.fill( ) << endl;
}
a
xxxxa
x

basic_ios::good

指示流处于良好状态。

bool good() const;

返回值

如果 rdstate == goodbit(未设置任何状态标志),则为 true,否则为 false

有关 goodbit 的详细信息,请参阅 ios_base::iostate

示例

有关使用 good 的示例,请参阅 basic_ios::bad

basic_ios::imbue

更改区域设置。

locale imbue(const locale& Loc);

参数

Loc
区域设置字符串。

返回值

先前的区域设置。

备注

如果 rdbuf 不是 NULL 指针,则成员函数将调用

rdbuf-> pubimbue(_ Loc)

不管怎样,它均将返回 ios_base::imbue(_ Loc)

有关详细信息,请参阅 pubimbueios_base::imbue

示例

// basic_ios_imbue.cpp
// compile with: /EHsc
#include <iostream>
#include <locale>

int main( )
{
    using namespace std;

    cout.imbue( locale( "french_france" ) );
    double x = 1234567.123456;
    cout << x << endl;
}

basic_ios::init

通过 basic_ios 构造函数调用。

void init(basic_streambuf<Elem,Traits>* _Sb, bool _Isstd = false);

参数

_Sb
用于存储输入或输出元素的标准缓冲区。

_Isstd
指定这是否是一个标准流。

备注

成员函数将值存储在所有成员对象中,因此:

basic_ios::int_type

traits_type::int_type 的同义词。

typedef typename traits_type::int_type int_type;

basic_ios::move

将所有值(指向流缓冲区的指针除外)从参数移动到当前对象。

void move(basic_ios&& right);

参数

right
从中移动值的 ios_base 对象。

注解

受保护成员函数将存储在 right 中的所有值移动到 *this,存储的 stream buffer pointer 除外,它将保留在 right 中不作更改并设置为 *this 中的 NULL 指针。 将存储的 tie pointer 设置为 right 中的 NULL 指针。

basic_ios::narrow

查找给定 char_type 的等效字符型。

char narrow(char_type Char, char Default = '\0') const;

参数

Char
要转换的 char

Default
如果未找到等效字符,则返回希望返回的 char

返回值

给定 char_type 的等效 char

注解

成员函数返回 use_facet<ctype<E>>(getloc()).narrow(Char, Default)

有关详细信息,请参阅 use_facetgetloc

示例

// basic_ios_narrow.cpp
// compile with: /EHsc
#include <ios>
#include <iostream>
#include <wchar.h>

int main( )
{
    using namespace std;
    wchar_t *x = L"test";
    char y[10];
    cout << x[0] << endl;
    wcout << x << endl;
    y[0] = wcout.narrow( x[0] );
    cout << y[0] << endl;
}

basic_ios::off_type

traits_type::off_type 的同义词。

typedef typename traits_type::off_type off_type;

basic_ios::operator void *

指示流是否仍处于良好状态。

operator void *() const;

返回值

仅在 fail 时,该运算符才会返回 NULL 指针。

示例

// basic_ios_opgood.cpp
// compile with: /EHsc
#include <iostream>

int main( )
{
    using namespace std;
    cout << (bool)(&cout != 0) << endl;   // Stream is still good
}
1

basic_ios::operator!

指示流是否完好无损。

bool operator!() const;

返回值

返回与调用 basic_ios::fail 相同的值

示例

// basic_ios_opbad.cpp
// compile with: /EHsc
#include <iostream>

int main( )
{
    using namespace std;
    cout << !cout << endl;   // Stream is not bad
}
0

basic_ios::operator bool

允许将 basic_ios 对象作为作 bool。 禁用自动类型转换以防止产生常见的意外副作用。

explicit operator bool() const;

备注

如果流没有错误,则返回 true;否则返回 false

basic_ios::pos_type

traits_type::pos_type 的同义词。

typedef typename traits_type::pos_type pos_type;

basic_ios::rdbuf

将流路由到指定缓冲区。

basic_streambuf<Elem, Traits> *rdbuf() const;
basic_streambuf<Elem, Traits> *rdbuf(
basic_streambuf<Elem, Traits>* _Sb);

参数

_Sb
流。

备注

第一个成员函数将返回存储的流缓冲区指针。

第二个成员函数在存储的流缓冲区指针中存储 _Sb,并返回以前存储的值。

示例

// basic_ios_rdbuf.cpp
// compile with: /EHsc
#include <ios>
#include <iostream>
#include <fstream>

int main( )
{
    using namespace std;
    ofstream file( "rdbuf.txt" );
    streambuf *x = cout.rdbuf( file.rdbuf( ) );
    cout << "test" << endl;   // Goes to file
    cout.rdbuf(x);
    cout << "test2" << endl;
}
test2

basic_ios::rdstate

读取流错误状态。

iostate rdstate() const;

返回值

存储的流状态信息。

示例

// basic_ios_rdstate.cpp
// compile with: /EHsc
#include <iostream>
#include <fstream>
using namespace std;

void TestFlags( ios& x )
{
    cout << ( x.rdstate( ) & ios::badbit ) << endl;
    cout << ( x.rdstate( ) & ios::failbit ) << endl;
    cout << ( x.rdstate( ) & ios::eofbit ) << endl;
    cout << endl;
}

int main( )
{
    fstream x( "c:\test.txt", ios::out );
    x.clear( );
    TestFlags( x );
    x.clear( ios::badbit | ios::failbit | ios::eofbit );
    TestFlags( x );
}
0
0
0

4
2
1

basic_ios::setstate

设置指定的流错误标志(当前设置的流错误状态标志保持不变):

flag 说明
goodbit 无错误
badbit 无法恢复的错误
failbit I/O 操作失败,例如格式设置或提取错误
eofbit 流已到达文件末尾
void setstate(iostate _State);

参数

_State
要设置的其他标志。

备注

该成员函数实质上调用 clear(_ state | rdstate)

有关详细信息,请参阅 clearrdstate

示例

// basic_ios_setstate.cpp
// compile with: /EHsc
#include <ios>
#include <iostream>
using namespace std;

int main( )
{
    bool b = cout.bad( );
    cout << b << endl;   // Good
    cout.clear( ios::badbit );
    b = cout.bad( );
    // cout.clear( );
    cout << b << endl;   // Is bad, good
    b = cout.fail( );
    cout << b << endl;   // Not failed
    cout.setstate( ios::failbit );
    b = cout.fail( );
    cout.clear( );
    cout << b << endl;   // Is failed, good
    return 0;
}
0
1

basic_ios::set_rdbuf

分配流缓冲区作为此流对象的读取缓冲区。

void set_rdbuf(
basic_streambuf<Elem, Tr>* strbuf)

参数

strbuf
要成为读取缓冲区的流缓冲区。

注解

受保护的成员函数在流缓冲区指针中存储 strbuf。 它未调用 clear

basic_ios::tie

确保依次处理流。

basic_ostream<Elem, Traits> *tie() const;
basic_ostream<Elem, Traits> *tie(
basic_ostream<Elem, Traits>* str);

参数

str
流。

返回值

第一个成员函数返回存储的绑定指针。 第二个成员函数将 str 存储在绑定指针中,并返回其先前的存储值。

备注

tie 导致两个流同步,使得一个流上的操作在另一个流上的操作完成之后发生。

示例

在以下示例中,通过将 cin 绑定到 cout,可保证 Enter a number: 字符串在该数字本身从 cin 提取之前进入控制台。 这消除了当读取相应数字时,“输入数字:”字符串仍在缓冲区中的可能性,使得我们确定用户实际上会得到一些提示来进行响应。 默认情况下,cincout 绑定在一起。

#include <ios>
#include <iostream>

int main( )
{
    using namespace std;
    int i;
    cin.tie( &cout );
    cout << "Enter a number:";
    cin >> i;
}

basic_ios::traits_type

模板参数 Traits 的同义词。

typedef Traits traits_type;

basic_ios::widen

查找给定 char 的等效 char_type

char_type widen(char Char) const;

参数

Char
要转换的字符。

返回值

查找给定 char 的等效 char_type

备注

成员函数返回 use_facet<ctype<E>>(getloc).widen(Char)

有关详细信息,请参阅 use_facetgetloc

示例

// basic_ios_widen.cpp
// compile with: /EHsc
#include <ios>
#include <iostream>
#include <wchar.h>

int main( )
{
    using namespace std;
    char *z = "Hello";
    wchar_t y[2] = {0,0};
    cout << z[0] << endl;
    y[0] = wcout.widen( z[0] );
    wcout << &y[0] << endl;
}

basic_ios::swap

将此 basic_ios 对象中的值与另一个 basic_ios 对象中的值进行交换。 但是,不会交换指向流缓冲区的指针。

void swap(basic_ios&& right);

参数

right
用于交换值的 basic_ios 对象。

备注

受保护的成员函数交换存储在 right*this 中的所有值,存储在 stream buffer pointer 中的值除外。

另请参阅

C++ 标准库中的线程安全
iostream 编程
iostreams 约定