文字列をストリームのように扱えるクラスがstrstream
std::strstream - cppreference.com
入出力演算子で、文字列を追加したりできる。
(ただし、随分前から非推奨になっているので、
basic_istringstream - cpprefjp C++日本語リファレンス
現在では、こちらを使うべきである。)
コンストラクタにバッファを指定すれば、そのバッファを使うし、そうでなければ動的にバッファを用意する。
今回は、自前でバッファを用意する。
bufを全て'X'で埋める。
そのbufをssに指定する。
ssに"ABC"を出力する。
ssをそのまま出力できないので、紐付いているbufを出力。
$ vi strstream.cpp $ g++ strstream.cpp -o strstream In file included from /usr/include/c++/8/backward/strstream:50, from strstream.cpp:4: /usr/include/c++/8/backward/backward_warning.h:32:2: 警告: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. [-Wcpp] #warning \ ^~~~~~~ $ ./strstream buf = ABCXXXXXXX�� �� $
非推奨なので警告が出ている。
そして、なんとNULL終端されていない。
(Xの並びの最後ではなく、ABCの後ろがNULL終端されていないということ。)
Sample/cpp/strstream/strstream/src/strstream at master · bg1bgst333/Sample · GitHub