マニュアルページ ostream.3
名前
ostream - 書式つき / 書式なし出力
形式
#include <iostream.h>
typedef long streampos;
typedef long streamoff;
class unsafe_ios {
public:
// 公開された型
// ストリーム操作モード
enum open_mode {
in = 0x01, // 読み取り用にオープン
out = 0x02, // 書き込み用にオープン
ate = 0x04, // EOF までシークする
app = 0x08, // 追加モード: EOF 以降に追加
trunc = 0x10, // ファイルがあれば内容をクリア
nocreate = 0x20, // ファイルがなければオープンを失敗させる
noreplace= 0x40 // ファイルがあればオープンを失敗させる
};
// ストリームのシーク方向
enum seek_dir { beg=0, cur=1, end=2 };
// 書式化フラグ
enum {
skipws = 0x0001, // 入力の空白を読み飛ばす
left = 0x0002, // 左揃えで出力する
right = 0x0004, // 右揃えで出力する
internal = 0x0008, // 正負号や基数指示子の後のパディング
dec = 0x0010, // 10 進数の変換
oct = 0x0020, // 8 進数の変換
hex = 0x0040, // 16 進数の変換
showbase = 0x0080, // 基数指示子を出力
showpoint = 0x0100, // 小数点を強制的に使用 (浮動小数点型での出力時)
uppercase = 0x0200, // 16 進数を大文字で出力
showpos = 0x0400, // 正の整数に "+" 記号を追加
scientific= 0x0800, // 1.2345E2 浮動小数点表記を使用
fixed = 0x1000, // 123.45 浮動小数点表記を使用
unitbuf = 0x2000, // 挿入後すべてのストリームをフラッシュ
stdio = 0x4000 // 挿入後、標準出力や標準エラーを
// フラッシュ
};
// 残りの部分については ios(3CC4)を参照 ...
};
class unsafe_ostream : virtual public unsafe_ios {
public:
// 公開された関数
// 書式化されていない入力関数
unsafe_ostream& put(char c);
unsafe_ostream& write(const char* ptr, int len);
// ワイド文字
unsafe_ostream& put(wchar_t wc);
unsafe_ostream& write(const wchar_t * wptr, int len);
// その他の関数
int opfx();
void osfx();
unsafe_ostream& flush();
unsafe_ostream& seekp(streampos pos);
unsafe_ostream& seekp(streamoff offset, unsafe_ios::seek_dir from);
streampos tellp();
public:
// 公開された演算子関数
unsafe_ostream& operator<< (char);
unsafe_ostream& operator<< (unsigned char);
unsafe_ostream& operator<< (short);
unsafe_ostream& operator<< (unsigned short);
unsafe_ostream& operator<< (int);
unsafe_ostream& operator<< (unsigned int);
unsafe_ostream& operator<< (long);
unsafe_ostream& operator<< (unsigned long);
unsafe_ostream& operator<< (float);
unsafe_ostream& operator<< (double);
unsafe_ostream& operator<< (const char* buf);
unsafe_ostream& operator<< (void* ptr);
unsafe_ostream& operator<< (streambuf* sbufp);
unsafe_ostream& operator<< (unsafe_ostream& (*manip)(unsafe_ostream&));
unsafe_ostream& operator<< (unsafe_ios& (*manip)(unsafe_ios&));
public:
// ワイド文字
unsafe_ostream& operator<< (wchar_t);
unsafe_ostream& operator<< (const wchar_t*);
public:
// 公開されたコンストラクタ
unsafe_ostream(streambuf* sbufp);
};
class ostream : virtual public ios, public unsafe_ostream {
public:
// 書式化されていない出力関数
ostream& put(char);
ostream& write(const char* ptr, int n);
ostream& write(const unsigned char* ptr, int n);
// ワイド文字
ostream& put(wchar_t);
ostream& write(const wchar_t *, int);
// その他の関数
int opfx();
int osfx();
ostream& flush();
ostream& seekp(streampos);
ostream& seekp(streamoff, seek_dir);
streampos tellp();
public:
// 公開された演算子関数
ostream& operator<<(char);
ostream& operator<<(unsigned char);
ostream& operator<<(short);
ostream& operator<<(unsigned short);
ostream& operator<<(int);
ostream& operator<<(unsigned int);
ostream& operator<<(long);
ostream& operator<<(unsigned long);
ostream& operator<<(float);
ostream& operator<<(double);
ostream& operator<<(const char*);
ostream& operator<<(void*);
ostream& operator<<(streambuf*);
ostream& operator<<(ostream& (*)(ostream&));
ostream& operator<<(ios& (*)(ios&));
public:
// ワイド文字
ostream& operator<< (wchar_t);
ostream& operator<< (const wchar_t*);
public:
// 公開されたコンストラクタ
ostream(streambuf* sbufp);
};
class ostream_withassign : public ostream {
public:
ostream_withassign();
ostream_withassign& operator= (ostream& ostr);
ostream_withassign& operator= (streambuf* sbufp);
};
extern ostream_withassign cout;
extern ostream_withassign cerr;
extern ostream_withassign clog;
ios& dec(ios&);
ios& hex(ios&);
ios& oct(ios&);
ostream& endl(ostream&);
ostream& ends(ostream&);
ostream& flush(ostream&);
unsafe_ios& dec(unsafe_ios&) ;
unsafe_ios& hex(unsafe_ios&) ;
unsafe_ios& oct(unsafe_ios&) ;
unsafe_ostream& endl(unsafe_ostream& i) ;
unsafe_ostream& ends(unsafe_ostream& i) ;
unsafe_ostream& flush(unsafe_ostream&) ;
機能説明
クラス ostream は、対応する streambuf への書式つきおよび書式
なしのデータの挿入 (出力) をサポートします。
クラス unsafe_ostream は、以下に示す機能のすべてを実装します
が、 複 数のスレッドからの同時アクセスに対する保護は行いませ
ん。クラス ostream は、 unsafe_ostream の各メンバー 関 数 を
mutex ロックを使用することにより、マルチスレッド環境での安全
性を保証します。MT-safe クラスを使用するだけでは、マル チ ス
レッド環境で使用しても安全なアプリケーション動作は保証されま
せん。同時に稼働している複数のスレッド間で入出力スト リー ム
・オブジェクトを共有することの詳細については、『C++ ライブラ
リ・リファレンス』の 4 章「マルチスレッド環境での従 来 型 の
iostream ライブラリの使用」を参照してください。
コンストラクタと代入
ostream(sbufp)
sbufp が指す streambuf をストリームに対応づけ、ios 状態
を初期化します。
ostream_withassign()
初期化を行いません。
ostream_withassign osw = sbufp
sbufp が指す streambuf を osw に対応づけ、osw を完全 に
初期化します。
ostream_withassign osw = ostr
ostr に対応する streambuf を osw に対応づけ、osw を完全
に初期化します。
注意:
ostream_withassign クラスのローカルオブジェクトは必ず初
期化してください。
ワイド文字のサポート
ストリームにワイド文字を出力するすべての操作においては、現在
の ロ ケー ル の 複 数 バ イト表現に従っていない文字があると
ios::failbit がセットされ、出力が停止します。また、ワイド 文
字の出力時には常に opfx() が呼び出されます。
出力用接頭辞 /
接尾辞関数
int i = ostr.opfx()
すべての挿入操作に共通の準備を行います。ostr のエラー状
態 が ゼロ以外のとき、opfx は直ちにゼロを返します。スト
リームが ostr に結合されていれば (ios(3CC4) の tie を参
照)、そのストリームはフラッシュします。関数 opfx の戻り
値は、何らかのエラーが検出された場合はゼロ、それ以外 の
場 合はゼロ以外です。挿入関数を独自に定義する場合は、最
初に opfx を呼び出してください。
ostr.osfx()
挿入操作のまとめの処理を行います。ios::unitbuf がセット
さ れていれば、ストリームをフラッシュします。ios::stdio
がセットされていれば、stdout と stderr をフラッシュしま
す (ios(3CC4) 参照)。戻り値はありません。定義済みの挿入
関数はすべて osfx を呼び出しますが、書式なし出力関数 は
こ の 呼 び出しを行いません。挿入関数を独自に定義する場
合、制御を戻す前に osfx を呼び出してください。
書式つき出力 (挿入) 関数
書式つき出力関数は opfx() を呼び出します。opfx() の戻り値 が
ゼ ロのとき、それ以上の処理は行いません。また、opfx() が正常
終了した場合、制御を戻す前に osfx() を呼び出します。
ostr << sbufp
ostr.opfx() の戻り値がゼロ以外のと き、 sbufp が 指 す
streambuf から抽出できるすべての文字を ostr に挿入しま
す。パディングは行いません。戻り値は ostr への 参 照 で
す。 この関数を使用すればストリームを効率よくコピーでき
ますが、その場合にコピー先とコピー元のストリームがと も
に結合されていないことを確かめる必要があります。
使用例 :
#include <iostream.h>
main()
{ // cin に cout をコピー
cin.tie(0);
cout.tie(0);
cout << cin.rdbuf(); // rdbuf については ios(3CC4) を参照
return 0;
}
ostr << x
ostr.opfx(0) の戻り値がゼロ以外のとき、x を表している文
字を ostr に挿入します。opfx の戻り値がゼロのときは何も
行いません。検出されたエラーはすべて ostr のエラー状 態
に 記録されます。これらの関数は常に ostr への参照を返し
ます。独自の関数を定義する場合は、次の形式に従ってく だ
さい。
ostream& operator<< (ostream&, SomeType)
さらに、上記の原則に従う必要があります。
x の型と ostream の書式状態 (ios(3CC4) 参照) により、変
換 と 挿 入 の 具体的な内容が決定します。これらの関数は
ostream の状態を変更しません。ただし、変数 width は、書
式 つきの挿入が行われるたびにゼロに再設定されます。定義
済みの書式つき挿入関数とその変換規則は次のとおりです。
char, unsigned char, signed char
文字 x を変換しないで ostr に挿入します。
wchar_t
ワイド文字を複数バイト表現に変換し、ストリームに挿入 し
ます。ios::failbit をセットし、ワイド文字が現在のロケー
ルの複数バイト表現を持っていない場合は停止します。
short, unsigned short
int, unsigned int
long, unsigned long
これらの型は、先頭にゼロのない数字の列で表現されま す。
ios::oct が セットされている場合は 8 進数、ios::hex が
セットされている場合は 16 進数、ios::dec がセットされて
いるか、これらのどのビットもセットされていない場合は 10
進数に変換されます。10 進変換の場合、x が 正 で、 か つ
ios::showpos が セッ ト されていれば、先頭にプラス符号
(`+') が付けられます。また、x が負のときは先頭にマイ ナ
ス符号 (`-') が付けられます。8 進変換と 16 進変換は「符
号なし」で扱われ、符号は含まれません。ios::showbase が
セットされている場合、8 進変換の場合は先頭に `0'、16 進
変換の場合は ios::uppercase がセットされているかどう か
によって、先頭に ``0x'' または ``0X'' が付けられます。
float, double
x の 値 は precision、 width、 ios::scientific、
ios::fixed、 ios::uppercase の現在の値に従って変換され
ます。ios(3CC4) を参照してください。
char*
x が指す位置から最初の NULL (0) 文字までの文字の列で 表
現されます。ただし、NULL 文字は含みません。
wchar_t*
最初のワイド文字 0 までの配列に格納されたワイド文字の複
数 バイト表現を挿入します。現在のロケールの複数バイト表
現に従っていないワイド文字があれば、ios::failbit をセッ
ト して停止します。パディングは、バイト単位でなく、文字
単位のフィールド幅にもとづいて行われます。現在の実装 で
は、パディング文字は常に単一バイトです。
void*
このポイン タ は、 そ の デー タ 型 が int で あ り、
ios::showbase と ios::hex がセットされている場合と同様
に変換されます。
書式なし出力 (挿入) 関数
これらの関数は、opfx() や osfx() を呼び出しません。
ostr.put(c)
文字 c を ostr に挿入します。操作に失敗した場合はエラー
状態をセットします。戻り値は常に ostr です。
ostr.put(wc)
ワイド文字の複数バイト表現を ostr に入れます。操作に 失
敗した場合はエラー状態をセットします。戻り値は常に ostr
です。
ostr.write(ptr, len)
ptr が指す char 型配列の先頭から len 文字を正確に ostr
に 挿入します。操作に失敗した場合はエラー状態をセットし
ます。戻り値は常に ostr です。
ostr.write(wptr, len)
wptr が指す文字を先頭に、len で指定された数のワイド文字
を複数バイト表現で ostr に挿入します。
位置決め関数
これらの関数は ostream に対応する streambuf の出力ポインタを
操 作します。詳細については、 sbufpub(3CC4) を参照してくださ
い。複数バイト入力操作を行なった場合、tellg() で取得した値が
streambuf の入力ポインタと異なる場合があります。しかし、その
場合でも、istream 上ではすべての操作とシークが正しく同期しま
す。
ostr.seekp(pos)
ostr.seekp(off, dir)
出力ポインタの位置を設定し、ostr への参照を返します。
streampos pos = ostr.tellp()
出力ポインタの現在の位置を返します。
その他の関数
ostr.flush()
対応する streambuf に格納されているすべての文字をフラッ
シュします (たとえば、出力ファイルに書き込みます)。戻り
値は ostr への参照です。
定義済みマニピュレータ
マニピュレータは、外見上、挿入または抽出されたオブジェクトと
して使用されることがありますが、実際はストリームの状態を変更
するだけです。詳細については、 manip(3CC4) と ios(3C++) を参
照してください。出力ストリーム用に、以下のマニピュレータが定
義されています。
ostr << manip
manip(ostr) の呼び出しと等価です。
ostr << dec
ostr の変換基数を 10 に設定します。
ostr << oct
ostr の変換基数を 8 に設定します。
ostr << hex
ostr の変換基数を 16 に設定します。
ostr << endl
改行を挿入して 1 つの行を終了し、ostr をフラッシュし ま
す。
ostr << ends
ostr に NULL (0) 文字を挿入して文字列を終了します。
ostr << flush
ostr.flush() の呼び出しと等価です。
関連項目
ios.intro(3CC4)、 ios(3CC4)、 manip(3CC4)、 sbufpub(3CC4)、
『C++ ライブラリ・リファレンス』の第 3 章「iostream ライブラ
リ」および第 4 章「マルチスレッド環境での従来型の iostream
ライブラリの使用」