分享方式:


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 類別。

Typedefs

類型名稱 描述
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。 使用good、、 badeof和 測試這些旗標fail

成員函式會以下列項目取代預存的資料流狀態資訊:

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是非零,函式會以 自變數 rdstate有效呼叫 clear 。 它會傳回 *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 儲存在例外狀況遮罩中,並傳回其先前的預存值。 儲存新的例外狀況遮罩可能會擲回例外狀況,就像呼叫 clearrdstate )。

範例

// 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;

傳回值

true 如果 rdstate & (badbit|failbit) 為非零,則為 ,否則 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;

傳回值

true 如果未 rdstate == goodbit 設定任何狀態旗標,則為 , 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 ,並設定為 NULL 中的 *this指標。 儲存的 tie pointer 會設定為 NULL 中的 right指標。

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

設定指定的資料串流錯誤旗標(目前設定的數據流錯誤狀態旗標保持不變):

旗標 描述
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 會使兩個資料流同步;因此,當其中一個資料流的作業完成之後,就會進行另一個資料流的作業。

範例

在下列範例中,藉由系結 cincoutEnter 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 約定