配列ベースの入出力
配列ベースの入出力は.
#include <strstream>
をインクルードする必要がある.
そもそも配列ベースの入出力ってなに?
出力用の文字配列を使用するときは,つぎのコンストラクタで出力ストリームオブジェクトを生成する.
ostream ostr(char* buf, streamsize size, openmode mode = ios::out);
配列に書き込まれた文字数を調べるには,
streamsize pcount();
を使う.
これは終端文字'\0'も含んだ文字数.
読み込み用のストリームオブジェクトを
istream istr(const char* buf);
で生成する.
ここで,bufは読み込む配列のポインタを示す.
入力を読み取って配列の最後に行くとeof()はtrueを返す.
入出力用のストリームオブジェクトをつくるには,
strstream ostr(char* buf, streamsize size, openmode mode = ios::in | ios::out);
あとは普通のストリームのように書くだけ.
また,streamsizeとかいうわけわかんない型を...
intじゃだめなん?足りないのかな?
#include <iostream> #include <strstream> using namespace std; int main(){ char buf[255]; ostrstream ostr(buf, sizeof buf); ostr << "腹減ったあああああああああああああああああああ\n"; ostr << "ビール飲みたい.\n"; cout << buf; return 0; }
と思ったら...
コンパイルで警告.
warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C standard. Examples include substituting the<X> header for the <X.h> header for C includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated. [-W#warnings]#warning This file includes at least one deprecated or antiquated header. \^1 warning generated.
くそ!すでに時代遅れなものらしい!
新しいのは第14章でやるってよ!